Wordpress' work or execution flow manipulating $wpdb

I have read about overwriting global variable $wpdb and connecting to a second database this way ( Search another database with wp_query using new wpdb ) so one could e.g. use WP_Query on 2nd DB. After that $wpdb is "restored" with $wpdb_backup.

See code below from @Tom J Nowell

global $wpdb;
// backup wpdb
$wpdb_backup = $wpdb;

// new db
$newdb = new wpdb($user, $password, $db, $host);

// pretend newdb is actually wpdb
$wpdb = $newdb;

// do stuff

// reset wpdb back
$wpdb = $wpdb_backup;

It's a theoretical question, maybe independant of hooks, only focused on execution-flow. Will PHP (or WP engine in general) primary execute and finish this script-part, where $wpdb 's parameters are set to 2nd database's connection details and after doing stuff on 2nd DB will it later switch back to old $wpdb ($wpdb_backup) before a second website visitor clicks a post link right before $wpdb is changed (or any other possible action handling DB1) and PHP reacts after $wpdb is changed to $newdb?

If so, in this scenario, WP displays a post from DB2 though the visitor expects (well, it's probably less him - it's WP) the certain Post from DB1. If such a post does not exist in DB2 WP throws an error.

Am I wrong? Thank you for your explanations.

edit Is it about priority with hooks?

I know there is add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] ); for hook priority.


edit2 Short question: Does WP execute one action after another or does it handle them simultaneously (so that there can be conflicts manipulating $wpdb and after doing some stuff change it back)?

Solutions

I am not sure if I understand your question right but you seem to misunderstand a bit how PHP requests are typically executed.

$wpdb is not persistent, it belongs to specific page load and only exists while that page load is being performed. Unless your code always changes it - independent second (third, etc) page load will have its own instance of database connection .

Same applies to actions - they are executed in the context of specific page load, not in any kind of global persistent way.

Similar questions

manipulating tinymce in Wordpress
I need to be able to extract, manipulate and update the text in wordpress's tinymce #content textbox.The code is coded in a wordpress plugin. The below post helps but i am unable to comment or contact the original creator to ask him further questions. Having 1 points I cant practically do anything except ask questions. Let me know if i am doing thi...
Manipulating data in WordPress Thickbox with jQuery
I need to be able to have a modal pop up to make quick edits to data in the WordPress back end. Using thickbox I have the modal working, and to create a new entry works just fine. I want to use the same modal for editing and just use jQuery to change some of the data. This is the code I use for my edit link This is the code for the thickbox This is...
manipulating wordpress "the_content"
I'm working on a WP site. For the article page, I use <?php the_content(); ?> to pull in the content of the post page that I'm on. I need to manipulate the first letter of the first paragraph. Fortunately for me, that first paragraph seems to have a unique class of excerpt. I'm able to make it big and change the font, etc, but an issue arises...
Manipulating a PHP variable inside WP_Query in WordPress
* This question may be quite long so hope to bear with me * Was trying to group address under a particular city dynamically. Layout sample: So based on the sample layout above, Cupertino belongs to CA and so is displayed below it. The city is selected inside an ACF group. I tried to query the cities by the following code: The value of the $i variab...
Manipulating Media uploader
Ok. Going to try and explain this as best as I can, so bare with me. Anyway, I'm trying to include the default media uploader as a part of my plugin. Currently, I've successfully managed to use the filter attachment_fields_to_edit to hide most of the input fields, leaving only the title & alternate text fields, alongside a custom submit button....
Manipulating menu HTML
My site's menu uses the built-in menu under Appearance >> Menus. I want to make a very simple change: I want to wrap each of my menu's second-level ULs in a DIV. What's the simplest way to accomplish this?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .