Make WordPress Core

Opened 7 years ago

Last modified 2 years ago

#40513 new enhancement

switch_to_blog should accept a WP_site as param

Reported by: spacedmonkey's profile spacedmonkey Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.0
Component: Networks and Sites Keywords: has-patch needs-unit-tests needs-refresh
Focuses: multisite Cc:


The switch_to_blog function currently only accepts a blog id. By passing a WP_Site object where available, it means switch has more context. The WP_site object has all the site information, including the network id.

Attachments (3)

40513.diff (2.2 KB) - added by spacedmonkey 7 years ago.
40513.2.diff (6.2 KB) - added by spacedmonkey 7 years ago.
40513.3.diff (6.5 KB) - added by spacedmonkey 7 years ago.

Download all attachments as: .zip

Change History (13)

7 years ago

#1 @MaximeCulea
7 years ago

  • Keywords has-patch 2nd-opinion added

Hi, this seems nice.

7 years ago

#2 @spacedmonkey
7 years ago

40513.2.diff has some examples of where we can use this change in core. #40511 will help.

#3 @flixos90
7 years ago


We should have a close look at the parameter names and documentation in order to clarify that $site_object may not actually contain a site object. I have to admit that appears a bit confusing.

Maybe this is a good time to introduce a new switch_site action and only run it if an object is passed. If a plugin developer then wants to do something with the site object, they'd need to use the switch_site action. If we ever introduce a function switch_to_site() or so, we could then have it require an object - this is of course future stuff, but such an action would bring something like forward compatibility. :)

7 years ago

#4 @spacedmonkey
7 years ago

I agree. Naming of variables and docs do need work here. @DrewAPicture is the best man to look at this.

I don't agree about a new action. The switch_blog action is fired in switch_to_blog and in the restore. In 40513.3.diff I now pass $current_blog in the restore function. If doing this is going to be useful, we have to pass it to the switch_blog action.

This ticket was mentioned in Slack in #core-multisite by spacedmonkey. View the logs.

7 years ago

This ticket was mentioned in Slack in #core-multisite by spacedmonkey. View the logs.

7 years ago

#7 @DrewAPicture
7 years ago

  • Keywords 2nd-opinion removed

Hi, sorry missed the ping. Yeah, I'd probably just simplify it down to $site or $new_site. Separately, I think I'd probably recommend doing the get_site() call inside of switch_to_blog() as it can already accept an object or int – and it would follow with core style on similar usage, such as with how get_post() is used inside of functions all the time.

#8 @johnbillion
3 years ago

  • Keywords needs-unit-tests needs-refresh added
  • Milestone changed from Awaiting Review to Future Release

#9 @millerf01
2 years ago

#55022 was marked as a duplicate.

Note: See TracTickets for help on using tickets.