[resolved] Globalizing $wpdb in multisite (14 posts)

  1. Diegobr
    Posted 3 years ago #

    Hi all,

    I am writting my first plugin. When I try to globalize the $wpdb object from an internal WordPress multisite dashboard (the code is embedded in the dashboard page), it works, i.e., I can run "$wpdb->blogid", and it shows the current blog ID, different depending which blog dashboard i run it from.

    But when I run this "$wpdb->blogid" from an external script, triggered from a dashboard button, it always shows blogid = 1, whatever the blog I run it from.

    This is the script:

    define('__ROOT__', dirname(dirname(dirname(dirname(__FILE__)))));
    			global $wpdb;
    			$ident = $wpdb->blogid;
    			$pref = $wpdb->prefix;
    			echo $ident;
    			echo $pref;
    			echo get_site_url( get_current_blog_id() );

    I am becoming crazy! If I run it from inside a WordPress dashboard, it shows the right blog ID, and the prefix like "wp_#". But If I run it as a external script, it always shows blogid = 1, and prefix = "wp_".

    It seems that when $wpdb gets globalized, it gets configured as if it was the main site...

    Any help is appreciatted!! thank you

  2. Diegobr
    Posted 3 years ago #

    Thank you Ipstenu,

    The variable I need global (and is supposed to be) is $wpdb. But something happens in my external script, that doesn´t gets $wpdb globalized right, it always point to the main blog of the multisite. But if I embed the above code in a blog admin dashboard, it works well, and points to the current blog...

  3. But when I run this "$wpdb->blogid" from an external script,

    How are you defining the blogID?

  4. Diegobr
    Posted 3 years ago #

    I am not defining it. It is a Multisite variable of the $wpdb class: http://codex.wordpress.org/Class_Reference/wpdb

    It is supposed I should declare $wpdb as global, so this object gets all data from the current blog. In this blog there is an explanation:


    The point is, it works when I run the lines above from inside a WordPress page (a dashboard from a subsite). But it always points to the main site when I run the code above as a solely php file (note I include the load.php file, and I have checked this external script can access the WordPress functions)...

    Thanks for your time Ipstenu

  5. Diegobr
    Posted 3 years ago #

    It seems that when I declare $wpdb as global from an external script, it doesn´t know which blog it should be from...

  6. Well that would make sense. It's not (yet) connected to WP.

  7. Diegobr
    Posted 3 years ago #

    Yes, I suspect so... How should it get connected to WP for this $wpdb variable to recognize the current multisite blog?...

    As you can see, I am requiring the load.php at the begining of the php script, and I have verified that I have access to other wordpress objects. Indeed, I have access to the $wpdb, but it doesn´t seem to know where blog it is...

  8. It's a vicious circle.

    WP can't know what site it's on till you connect, but you're connecting and wanting the site right away. A plugin, however, being activated from within WP, should know what site it's on, so your constant mention of external scripts bewhilders me.

  9. Diegobr
    Posted 3 years ago #

    Hi Ipstenu,

    My php script is triggered from a button on the dashboard. So, as I am in the dashboard, hence me, the admin user, is logged in (Indeed I have checked that the script can detect the current logged user with the "wp_get_current_user()", and it shows the right logged user.

    So, it seems that my "external" script can detect the current user,
    can include the load.php and can use the wp functions. But the $wpdb doesn´t refers to the current database, but always to the main site database... whatever the blog I am...

  10. Diegobr
    Posted 3 years ago #

    As far as I can think, an "external" php script, can detect the current user because the cookies, but how does wordpress identifies the current blog? If i knew that, I would try to get that environment in my external php script...

    In other words, I know that when I instantiate $wpdb from a function embedded in a dashboard page, it points to the right current blog database. But when I instantiate it in an external script, it always point to the main site database. So, how can I make the script environment to know which blog it is on??...

  11. I don't think you're using 'external' the right way and you're confusing me..

    Where is the file?

  12. Diegobr
    Posted 3 years ago #

    Sorry for the delay,

    Thanks Ipstenu, finally i found what was my mistake. When I call a custom .php page from a subsite admin page, I have to use the subsite URL, not the real file location. When I do that, wordpress executes that .php page as in a subsite. I.e. $wpdb now points to the right subsite tables.

    If I call the .php page using the real URL (__root__./wp-content/plugins/myplugin/page.php), wordpress runs it as if it is a main blog script ($wpdb points to the main blog database). but if i call it as a subsite URL (__root__./SUBSITE/wp-content/plugins/myplugin/page.php), then $wpdb points to the subsite tables!

    Thanks for your time Ipstenu, I hope this helps others!

  13. OP came back with fix! ALL THE POINTS! :D

    Thanks Diegobr!

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.