Title: wp_switch_roles_and_user
Published: November 20, 2017
Last modified: May 20, 2026

---

# wp_switch_roles_and_user( int $new_site_id, int $old_site_id )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#wp--skip-link--target)

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#parameters)󠁿

 `$new_site_id`intrequired

New site ID.

`$old_site_id`intrequired

Old site ID.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#source)󠁿

    ```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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/ms-blogs.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/ms-blogs.php#L695)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/ms-blogs.php#L695-L706)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Roles::for_site()](https://developer.wordpress.org/reference/classes/wp_roles/for_site/)`wp-includes/class-wp-roles.php` |

Sets the site to operate on. Defaults to the current site.

  | 
| [wp_roles()](https://developer.wordpress.org/reference/functions/wp_roles/)`wp-includes/capabilities.php` |

Retrieves the global [WP_Roles](https://developer.wordpress.org/reference/classes/wp_roles/) instance and instantiates it if necessary.

  | 
| [wp_get_current_user()](https://developer.wordpress.org/reference/functions/wp_get_current_user/)`wp-includes/pluggable.php` |

Retrieves the current user object.

  | 
| [did_action()](https://developer.wordpress.org/reference/functions/did_action/)`wp-includes/plugin.php` |

Retrieves the number of times an action has been fired during the current request.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_switch_roles_and_user/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.9.0](https://developer.wordpress.org/reference/since/4.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_switch_roles_and_user%2F)
before being able to contribute a note or feedback.