Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Chris Andruszko
Chris Andruszko
18,392 Points

accessing $wpdb from a different WordPress database (getting a little desperate for an answer)

I use the following code to get the database prefix from the current WordPress database:

$db_prefix = $wpdb->base_prefix;

But now I need to get the prefix from an external mysql database for a different WordPress site. The connection to the other database is defined in $conn. The code I tried to use is:

$db_prefix_conn = $conn->base_prefix;

But that doesn't work. Can anyone tell me how to write a code to find the external database's prefix?

Any help is greatly appreciated. I've been asking around but no one seems to be able to give me a direct answer.

1 Answer

Hi Chris,

I haven't done this myself so can't confirm it's accuracy but is this the sort of thing you're looking for?

-Rich

Chris Andruszko
Chris Andruszko
18,392 Points

Thank you for the response. This solution seems to be setting a new prefix to use for the new $wpdb instead of using the current one. So, if the new prefix you set isn't the same as the current prefix, then it won't work. I need the code to actually get the current prefix being used for the external database.

The code I have now is:

<?php
global $new_wpdb;

define( 'BLOCK_LOAD', true ); 
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' ); 
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' ); 
$new_wpdb = new wpdb( $username, $password, $dbname, $servername); 
$new_wpdb->show_errors();
?>

The array for $new_wpdb can be printed and I can see the values for username, password, etc. But there's no values for the prefix or the base_prefix.

Hi Chris,

Sorry I couldn't be more helpful on this one but I haven't managed to find anything. Hope you manage to sort it and I'll post on here if I find a solution in the meantime :)

-Rich

Chris Andruszko
Chris Andruszko
18,392 Points

Thanks for trying to help though. It appears that what I'm trying to do isn't possible (or at least not reliable) unless both databases are on the same server. So I changed my approach. The user of the plugin already needs to enter their credentials to connect to the external database. So I just added another field to the form asking for their prefix. I also included information in the ReadMe on how to find the prefix in case the user doesn't know what it is. It wasn't the method I was hoping to use, but it works.