Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When switching site, also switch network as well #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

When switching site, also switch network as well #119

wants to merge 1 commit into from

Conversation

spacedmonkey
Copy link
Contributor

Currently when switching blog (site), the network is not changed. This can have issues when it comes to context aware function, that need the current network id to be correct. An example might be wp_update_network_site_counts. By switching network, it stops possible issues.

Doing a get_site on switch_to_blog, will add some overhead. On sites with object caching, it may result in another query. However, most times using switch_to_blog the wp_site_query class is also used like in network admin. wp_site_query will prime the wp_site object in cache (in without object caching). But there will be places where this isn't the case.

This PR includes tests, to prove it works. There maybe more tests that can be done.

fixes: #118

@JJJ
Copy link
Collaborator

JJJ commented Jun 8, 2017

I like it!

@spacedmonkey
Copy link
Contributor Author

@JJJ @rmccue Any chance this gets merged?


function switch_blog_and_network ( $new_blog, $prev_blog_id ){
$site_object = get_site( $new_blog );
if ( !($site_object instanceof WP_Site ) ){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to just ! $site_object.

@felixarntz
Copy link
Collaborator

I'm not sure switching the network should happen automatically, as this may introduce all sorts of unexpected behavior, particularly because plugins (unfortunately) don't expect the network to be switched.

To some degree I'd love for this to be the case, but due to the historic circumstances it may work better as an optional feature. Maybe we could introduce the action function, but not automatically hook it in? Then custom setups could decide on that, but it wouldn't be the default. Or there could be a switch_site_and_network() and restore_site_and_network() that hook in the filter, call switch_to_blog() and then unhook the filter again.

@JJJ
Copy link
Collaborator

JJJ commented Nov 24, 2022

Or we could have a function that toggles network-switching on/off, kind of like how caching has?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch network when switching site
3 participants