Filter Since 1.1.0 uncanny-learndash-toolkit

user_switching_switched_message

Filters the contents of the message that's displayed to switched users in the admin area. Filters the admin message shown to users after switching accounts, allowing customization of the notification.

add_filter( 'user_switching_switched_message', $callback, 10, 5 );

Description

Fires after a user has successfully switched. Developers can filter the admin notification message displayed to the switched user. This hook provides access to the current user, the original user, the URL to switch back, and a flag indicating if the switch just occurred.


Usage

add_filter( 'user_switching_switched_message', 'your_function_name', 10, 5 );

Parameters

$message (string)
The message displayed to the switched user.
$user (WP_User)
The current user object.
$old_user (WP_User)
The old user object.
$switch_back_url (string)
The switch back URL.
$just_switched (bool)
Whether the user made the switch on this page request.

Return Value

The filtered value.


Examples

<?php
/**
 * Modify the user switching message to include the time of the switch.
 *
 * @param string  $message         The message displayed to the switched user.
 * @param WP_User $user            The current user object.
 * @param WP_User $old_user        The old user object.
 * @param string  $switch_back_url The switch back URL.
 * @param bool    $just_switched   Whether the user made the switch on this page request.
 * @return string The modified message.
 */
function my_custom_user_switching_message( $message, $user, $old_user, $switch_back_url, $just_switched ) {
	// Only modify the message if the switch just happened on this page load.
	if ( $just_switched ) {
		// Get the current time in WordPress's timezone.
		$switch_time = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
		$message .= sprintf(
			esc_html__( ' You switched to %s at %s.', 'your-text-domain' ),
			esc_html( $user->user_login ),
			$switch_time
		);
	}

	return $message;
}
add_filter( 'user_switching_switched_message', 'my_custom_user_switching_message', 10, 5 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/includes/user-switching.php:457

*
					 * @param string  $message         The message displayed to the switched user.
					 * @param WP_User $user            The current user object.
					 * @param WP_User $old_user        The old user object.
					 * @param string  $switch_back_url The switch back URL.
					 * @param bool    $just_switched   Whether the user made the switch on this page request.
					 */
					$message = apply_filters( 'user_switching_switched_message', $message, $user, $old_user, $switch_back_url, $just_switched );

					echo wp_kses(
						$message,
						array(
							'a' => array(
								'href' => array(),
							),

Scroll to Top