Support » Fixing WordPress » Remotely connect to another WP Database

  • Hey everybody

    I’ve been working on a plugin which I’m going to pass onto a couple of websites. These websites have joined a “Network” we have made and the plugin adds a header bar and menu from the “Hub” website.

    Before I ask the question, here is what I have so far….

    DB_User = Database Username
    DB_Pass = Database Password
    DB_Name = Database Name

    // Save Current Database details
    $wpdb_old = wp_clone($GLOBALS[‘wpdb’]);
    $wpdb_new = &$GLOBALS[‘wpdb’];
    // Open Remote Connection
    $wpdb_new = new wpdb(‘DB_User’,’DB_Pass’,’DB_Name’,’localhost’);

    // Get Menu
    wp_nav_menu( array( ‘sort_column’ => ‘menu_order’, ‘container_class’ => ‘tpk_plugin_header_menu’, ‘menu_class’ => ‘tPKmenu’, ‘menu’ => ‘tPK_plugin’ ) );

    // Reset the query
    // Put the old wpdb back
    $wpdb_new = $wpdb_old;

    The code above works perfectly fine, as long as the website using it is on the same web server as the “Hub” website. Most of the websites that will use this code are on the same server and already using this code perfectly. Some of them are not and display no menu

    I know where the problem is, I just can’t work out what I need to replace it with.

    $wpdb_new = new wpdb(‘DB_User’,’DB_Pass’,’DB_Name’,’LOCALHOST’);

    What do I change “Localhost” to, that will allow sites running the plugin on another website to be able to access the data?

    I tried the IP address of the SQL server and it has little sucess. I’ve made sure that any sites I’m working with, have permission for remote access, but still nothing.

    What am I missing?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Have you checked the firewall on your server? That’s normally where connection issues like this occur. Remember that your hosting company can also have a hardware firewall that might be blocking the connection request before it even gets to your server so there’s a few more things then just allowing remote connection.

    One other thing that I’d like to point out is that the way you’re doing it is very (and I mean VERY) dangerous – if you’re not the only one that will ever use that code. As soon as you allow others to connecto to your WP database like that, you run the risk of anyone at all being able to read all of the data that’s contained in it. You can always try to set up the user with special permissions, but that’s not an easy thing to do unless you really understand how to work the MySQL permissions system.

    Thank you for your reply.

    After reading it, i realised how dangerous it could be. I have for obvious reasons stepped away from that method. I wish to say thank you for pointing out the obvious to me. Sometiomes you just need another pair of eye to point out clear issues that you have somehow over looked.

    Thank you again.

    Is there any other method for getting or sending the menu info from one site to another?

    There is, and it shouldn’t be too hard to do. Well, sort of…

    If it was me doing it, I’d set up a page that returns only the HTML code for the menu. That way the first site can call that URL using CURL or something similar, and include that in the site. All you need is a file that does something sort of like this…

    require_once ('../../wp-blog-header.php');
    wp_nav_menu (array ('theme_location' => 'primary'));
    die ();

    You’ll need to change the code above to suit your own system, but that’s the basics of it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Remotely connect to another WP Database’ is closed to new replies.