19 November 2010

How to access Wordpress Database Away from Wordpress Root Read more: How to access Wordpress Database Away from Wordpress Root | HackTweaks Under Creative Commons License: Attribution No Derivatives

WordPress by defauly supports globally announcing $wpdb to access the WordPress. But in case you are away from the WordPress root you'll need to redefine the database information to access WordPress.
Again after accessing the database you may need to redefine the database prefix, which will really eat your time and dynamic ability of your script. This will get even more complicated when you need define the table name dynamically.
Perhaps, we can avoid redefining the database and use wp-config.php instead.If your script is in same host, you can easily access wp-config by using file_get_contents()PHP function.

You must note that file_get_contents() doesn't fetches files in external server. For example, if you have defined a function in database.php at http://www.firsthost.com and you tried to read wp-config.php from http://www.anotherhost.com then it might return an error or nothing.
Let me show you how you can achieve this with a simple trick...First of all define a variable with the location of your wp-config file.

$wpconfig = 'http://www.example.com/wordpress/wp-config.php';

Then we'll use explode() function to the data we get from wp-config.php that will separate each word in new lines, \n
.Again we'll redefined the resulted array as $line.

foreach (explode("\n", file_get_contents($wpconfig)) as $line)

Up to here we've made wp-config.php readable for our script which is not in wordpress root directory.
Let's see what we usually have in wp-config.php 


There you can see define(DB_XXXX); function in separate line. Each of these line will be defined in array. Now we'll need to redefine that array in to a PHP function we'll do this by eval(); function.
Let's see how can we do this, we add new conditional statement with preg_match(); that will search for the define(); function with DB_ prefix in it. If it exists we'll finally evaluate it as a PHP using eval(); function.
Here is the code what've reached up to now. 


$wpconfig = 'D:\wamp\www\wordpress\wp-config.php';
   foreach (explode("\n", file_get_contents($wpconfig)) as $line) {
           if (preg_match('/define.+?DB_|table_prefix/', $line))
               eval($line);
   }


Now we've define(DB_USER); , define(DB_NAME); , and etc which contains all the necessary information to access your database.

Again we must verify that if anyone of them have defined value or not. If any one of them have some value defined, then probably all of the definitions must have values defined in it.

Let's use conditions to connect the MYSQL database. 
Then finally fetch the database.


 if (defined('DB_USER')) {
   $dbh = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
   mysql_select_db(DB_NAME, $dbh);
   $r = mysql_query("SELECT * FROM wp_designer WHERE ID = 1;",$dbh) or die(mysql_error());
   $row = mysql_fetch_array($r) or die(mysql_error());
     $column1 = $row['column1']; 
     $column2 = $row['column2'];
  }   



Don't use $wpdb here, since we haven't defined it globally and neither it is necessary. 
We can use PHP MySQL syntax instead.

Up to here, you'll have successful connection with the Wordpress database, with information derived from wp-config.php.
Hope this tutorial was helpful.
If you liked this post, share it with your friends by using the favorable icons below. If you got problem then don't delay to comment us. Subscribe our blog to get free updates, tips and tricks. You can also contact us by using contact us page if you have any problems related to wordpress or Blogger or HTML/CSS and PHP.

No comments:

Post a Comment