Documentation
SwitchUser
Developer Reference

WindCodex SwitchUser Developer Reference


Constants

ConstantValueDescription
SWITCHUSER_VERSIONe.g. '1.0.2'Current plugin version
SWITCHUSER_PLUGIN_FILE__FILE__Plugin file path
SWITCHUSER_PLUGIN_DIRplugin_dir_path()Plugin directory
SWITCHUSER_PLUGIN_URLplugin_dir_url()Plugin URL
SWITCHUSER_PLUGIN_BASEplugin_basename()windcodex-switchuser/windcodex-switchuser.php

Actions

ActionArgumentsDescription
switchuser_switched$data (array)Fires after switching into a user. $data contains actor_id, origin_id, target_id, ip and time (UTC)
switchuser_switched_back$data (array)Fires after switching back. $data contains actor_id, origin_id, from_id, ip and time (UTC)
switchuser_before_settings-Fires at the top of the SwitchUser settings page

SwitchUser also fires WordPress's standard wp_login action when it logs you into the target account and back.

Example - custom logging:

add_action( 'switchuser_switched', function ( array $data ) {
    error_log( sprintf( 'User %d switched into user %d from %s', $data['actor_id'], $data['target_id'], $data['ip'] ) );
} );

There are no filters.


Options

All settings are stored in the switchuser_settings option:

KeyTypeDefaultDescription
enable_user_switchingyes/noyesMaster switch
block_admin_targetsyes/nonoBlock switching into administrators
allowed_switcher_rolesstring''Comma-separated role slugs allowed to switch (empty = edit_users)
session_ttl_hoursint48Session Duration, 1–168
order_screen_enabledyes/noyesWooCommerce order screen box
profile_screen_enabledyes/noyesProfile screen button

PHP Methods

$core = SwitchUser_Core::instance();
 
// Can the current user start switching at all?
$core->current_user_can_switch(); // bool
 
// Can the current user switch into this account?
$core->can_switch_to_user( get_user_by( 'id', 42 ) ); // bool (takes a WP_User)