wp_switch_roles_and_user() WordPress Function

The wp_switch_roles_and_user() function allows a user to switch between multiple roles and users. This function is useful for sites that have multiple users with different roles.

wp_switch_roles_and_user( int $new_site_id, int $old_site_id ) #

Switches the initialized roles and current user capabilities to another site.


Parameters

$new_site_id

(int)(Required)New site ID.

$old_site_id

(int)(Required)Old site ID.


Top ↑

Source

File: wp-includes/ms-blogs.php

function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
	if ( $new_site_id == $old_site_id ) {
		return;
	}

	if ( ! did_action( 'init' ) ) {
		return;
	}

	wp_roles()->for_site( $new_site_id );
	wp_get_current_user()->for_site( $new_site_id );
}


Top ↑

Changelog

Changelog
VersionDescription
4.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.