Multisite Problems: Conflicts with Yoast SEO
-
Hello all!
I was recently using your WP Advanced Search plugin on a WordPress Multisite installation, and saw some issues with the free version of Yoast SEO (https://ww.wp.xz.cn/plugins/wordpress-seo/). I have verified that it is the WP Advanced Search plugin that is causing my troubles. Disabling the plugin fixes the problem.
The basic problem I encountered was that, when we were filling in SEO data for the pages, Yoast was not saving the data. This was because it was thinking that the site was switched to another blog via the “switch_to_blog()” function.
Searching through all of the plugins we have on our site, I saw some use of the switch_to_blog() function within yours, but it uses it in the following manner (this snippet is taken from WP Advanced Search’s install function):
$blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); foreach($blogids as $blog_id) { switch_to_blog($blog_id); WP_Advanced_Search_install_data($wpdb->prefix.$tableName); } switch_to_blog($original_blog_id);As I understand it, this is rather bad practice because, although you switch back to the original blog id after you finish your loop, the site still thinks that it is switched to another blog. Yoast uses a function “ms_is_switched()” to tell if the site is in this state and, if it is, it will not save any changes to the metadata the user has made, which was causing our previously mentioned issue. To avoid this, you should instead use the “restore_current_blog()” function.
A better solution would be the following:
$blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); foreach($blogids as $blog_id) { switch_to_blog($blog_id); WP_Advanced_Search_install_data($wpdb->prefix.$tableName); restore_current_blog(); }The “restore_current_blog()” function is the best solution for switching back to the main blog. Unfortunately, it shouldn’t be run outside of the loop, but should be run after each loop iteration. This will restore the current blog’s data to the site, and prevent these errors from happening.
Your plugin is great otherwise! It is simple to install and work with and, besides this one issue, it has been working great for us.
Thank you and have a great day. Please let me know if you have any other questions about my issue.
More information on this issue can be found at:
https://wordpress.stackexchange.com/questions/89113/restore-current-blog-vs-switch-to-blog
https://codex.ww.wp.xz.cn/WPMU_Functions/restore_current_blog
The topic ‘Multisite Problems: Conflicts with Yoast SEO’ is closed to new replies.