Issue with switch_to_blog
-
Hi, we’re currently running the latest version of WP, Buddypress, BadgeOS, and BadgeOS Community.
I noticed a weird bug: $blog_id gets messed up in a multisite installation in some scenarios. After tracing the issue I noticed that you’re polling for achievements in all the blogs(includes/user.php, line 389) by using switch_to_blog() in a loop, and then resetting to current blog by doing another switch_to_blog() after the loop.
This apparently causes an issue with the latest version of Buddypress, because they added a new function bp_get_object_terms (buddypress/bp-core/bp-core-taxonomy.php, line 63) that gets called at some point. In that function, it calls restore_current_blog(), which sets the blog to before the last time switch_to_blog() was called. It it determines the last switch by looking at global variable $GLOBALS[‘_wp_switched_stack’], which just contains an array of all the previously switched blog ID’s, and popped off the last element.
So in the case of site running BadgeOS, restore_current_blog() actually changes the current blog to the last polled blog.
In our case, it causes issues with our custom template since WP/BP starts looking at the wrong directory for our custom template.
I am not sure what the best solution is, but in our case by adding $GLOBALS[‘_wp_switched_stack’] = array(); after the last switch_to_blog() in badgeos_get_network_achievement_types_for_user() fixes the problem. However, that may not be the best solution because there might be legit reason why that switch stack should not be cleared out.
Please let me know if you need me to clarify anything.
The topic ‘Issue with switch_to_blog’ is closed to new replies.