Templates and pulling data from my own database (not wordpress one) - wordpress

I'm pretty new to wordpress. I have read a lot about templates, template pages, etc... and I would really apreciate if someone could confirm my thoughts, ideas about how to build my Homepage with wordpress (I won't be using it for blooging, but my enterprise webpage).
The main problem is that I need to have a page, where a little list of items will be shown. After one of them is clicked another page will be shown with information about the item like a pic, short description, etc... etc... This Item information page will be using a template page where data from my own CMS database has to be inserted in the template holes.
All examples I have seen in wordpress are about pushing data in wordpress database and showing it using WP API. But, what about if I just want to use wordpress as a template system, with all its plugins, etc... and pull the data needed to be shown from another database (our CMS)?. Could anyone suggest best practices for this, or a better aproach?.
Thanks in advance.

is your wordpress database in the same database as your own?
ie: you can access the wp tables along with all your other tables?
if so you can use the wp api, to connect,
using the normal connection methods..
if the tables are not in the same database but located under the same account on your host, you could use the same method shown in this thread...
$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");
echo "<ul>";
foreach ($rows as $obj) :
echo "<li>".$obj->Name."</li>";
endforeach;
echo "</ul>";
if the database is in another location, outwith the current server just change the localhost to your server ip and port to connect, ie:
$mydb = new wpdb('username','password','database','192.168.1.1:6807');
good luck
Marty

Related

Fetch data from external database to WordPress website

I am new to WordPress, I have an old website containing a simple database to my diner's menu, I am switching my website to WordPress and I need to use my old database, is their anyway I can use both databases? The one already in WordPress and my own database?
You can try something like this or refer to this forum: https://wordpress.org/support/topic/multiple-databases-16/
function seconddb() {
global $seconddb;
$seconddb = new
wpdb('Username','password','database name','localhost');
}
add_action('init','seconddb');

How to create a profile page in Wordpress Multisite

I can't find a way to create a front-end profile page for all users under Wordpress Multisite Network. I also wanted the profile page to be on the main site and not on sub-sites if possible. I don't want to use any social network plugins like buddypress.. I just want to create a page like http://mainsite.com/profile/username
Is there a way to accomplish this?
Maybe you might check out Buddypress which is a social layer on top of Wordpress and is able to do what you need (i.e. something like http://mainsite.com/profile/username).
Link: http://buddypress.org/
More specifically, in Buddypress the default behaviour is http://mainsite.com/members/username
I have noticed that you have edited the question and that you are looking for a way to accomplish this without any plugin. There are a series of tags in Wordpress to show the author's data. So basically you could create a page and then use the_author_meta tags. For example the following code echoes the first and last name of a user whose name is stored in $userID:
<?php
$pagename = $userID;
echo the_author_meta(user_firstname, $userID );
echo the_author_meta(user_lastname, $userID );
?>
Please note that the variable $pagename is empty in case you use a static page as home page, but it is not your case.
Source: https://codex.wordpress.org/Template_Tags/the_author_meta

Wordpress permalink from the database?

I am trying to access selected post information from my WordPress database to display on a second website which is not a WordPress site nor is it php, it is in fact an asp.net website that i want to display the information on.
I have a WordPress blog at blog.domain.com and another website at domain.com both running on different servers and I need to display the excerpt, title with link on my asp.net website for selected posts. I can easily grab the excerpt and title from the blog's database but the only problem is that the permalink is not in the database.
I had the idea that maybe if I place a short piece of code on every blog to insert the permalink to a new table, i could then grab that to use over on the other website. But that didnt work, perhaps I am not doing it right. This is the code I used;
<?php
global $wpdb;
$table_name = $wpdb->prefix . "posts_pages_url";
$wpdb->insert($table_name , array('post_id' => the_ID(), 'url' => the_permalink()));
?>
Some might say, "why dont you just use an RSS Feed?" Well if it comes down to it, I just might have to, but i would prefer not to as connecting to the database directly to access my selected posts is much more flexible.
I would be very grateful if anyone had any suggestions on how I can access the permalink via a database call.
Many thanks
You could use get_permalink() in your code, or link to the guid value in the DB - not ideal I know, but it should at least work.
EDIT:
Same goes for the_ID() - it should be changed to get_the_ID().
This code will get it for you if using Yoast
$q = "SELECT * FROM wp_yoast_indexable yi
WHERE object_type = 'post'
AND object_id = '$ref'";
$yis = $scDb->select($q);
$permaLink = "";
if(isset($yis[0]))
{
$permaLink = $yis[0]->permalink;
}

How can I display posts from the other sites in a WordPress multisite setup?

I have a small network of sites setup with WordPress 3.0's multisite feature. I would like to create another site which pulls certain posts from the various other sites to display. This new 'hub' site would seem like its own separate site to the user (with domain mapping), but its content is coming from the posts from the other sites.
How can I get posts from another site in a WordPress multisite setup? Can I query for posts based on the name of the site? The end result needs to be a collection of posts from the different sites sorted by date.
Thanks for your help.
I had the similar issue where I wanted to get posts from one blog and display it on an other I came up with the following solution which you could modify slightly to meet your needs if needed
<?php
global $switched;
switch_to_blog(2); //switched to 2
// Get latest Post
$latest_posts = get_posts('category=-3&numberposts=6&orderby=post_name&order=DSC');
$cnt =0;?>
<ul>
<?php foreach($latest_posts as $post) : setup_postdata($post);?>
<li>
<?php echo short_title('...', 7); ?>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site ?>
I'm also limiting the amount of words which is being out putted if you do not want this feature simple use
$post->post_title;
Hope it helps.
This wouldn't be terribly difficult to set up with direct database calls. You can query posts from any site on the install with the $wpdb object. See Displaying Posts Using a Custom Select Query for information on using a custom database query, but keep in mind that instead of selecting from $wpdb->posts you're going to need to access the specific site table you want posts from. On a default Wordpress 3 install, this would be wp_12_posts where 12 is the site id. The id can be found in the wp_blogs table, or by looking at the ID column in the Sites section of the admin menu.

Wordpress Plugin to Generate non-numeric slug / permalink for posts without titles? (1 post)

I've been looking for this all over, and simply cannot find it.
I have a blog that has no titles in its blog posts, but I'd like, for various usability reasons, to have the permalinks use the first few words from entries that do not have titles as the permalink slug.
ie, if the post on sample.com/blog is
Title: (no title)
Content: Ten Easy Ways to Lose Weight
The permalink could be sample.com/blog/ten-easy-ways-to-lose-weight.
Are there any plugins that do this? For the life of me, I cannot find one. (xposted to WP support, but no one is responding)
You could enter in titles, and then not display them in your view template.
I doubt there's anything like this already built for wordpress. To get your blog to do this, you have to write a plugin that does the following:
Generates the slug while checking
for uniqueness should you ever start
more than one entry with the same words
Processes URL requests to recognize slug permalinks and then updates the query step to locate the correct post in the database. This might involve a new db table of slugs (which would also help with the uniqueness issue)
In short, WP is designed to retrieve almost everything by keys, and to support slugs like this you'd have to create a new key type.
btw: if anything is retrieved by IDs (keys), it is technically not a permalink. so, wordpress probably fails in providing true permalinks.
ps: it's not that difficult to write an handler/dispatcher that would parse URL and takle out the unique permalink and then match it to the DB by the string (not by the key!).
something like:
$url=$_SERVER["REQUEST_URI"];
echo 'URL called: ',$url,'<br />';
$dispatchfile=$dispatcher->Dispatch($url);
if ($dispatchfile)
{
echo 'launching ',$dispatchfile,' inclusion<br />';
require($dispatchfile);
}
else
{
echo 'dispatcher failed to find module, will check physical file<br />';
if (file_exists($url)) echo 'dispatcher found physical file<br />';
else echo 'nada, throw 404!';
}
You can get a permalink redirect plugin from
http://scott.yang.id.au/code/permalink-redirect/
Works fine with WP2.71
It takes the Title and auto-creates a slug from that so you would have to manually enter the slug you wanted for each page if you have a Blank Title.
You should be able to hack Scott's PHP file (it is one page only) to look up the page code and select a portion of it to use as a slug though.
In addition, I solve incorrect page requests using a .htaccess rewrite file to bring up the index page upon an incorrect page request.
Download a copy of my rewrite file here
https://oulixes.com/htaccess_example.zip
Unzip the txt file and rename as .htaccess and upload into your root directory
Hope this helps!
Cheers,
Billy

Resources