do_action( ‘switch_locale’, string $locale, false|int $user_id )

Fires when the locale is switched.

Parameters

$localestring
The new locale.
$user_idfalse|int
User ID for context if available.

Source

do_action( 'switch_locale', $locale, $user_id );

Changelog

VersionDescription
6.2.0The $user_id parameter was added.
4.7.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Basic example of the hook for below operations.

    1. Updating the user meta with the new locale using the update_user_meta() function.
    2. Displaying a success message indicating the switched locale.
    // Define a callback function to execute when the switch_locale action is triggered
    function wpdocs_switch_locale_callback( $locale, $user_id ) {
        // Perform custom logic or tasks here based on the locale and user ID
        
        // Example: Update user meta with the new locale
        update_user_meta( $user_id, 'locale', $locale );
        
        // Example: Display a success message
        echo 'Locale switched to: ' . $locale;
    }
    
    // Hook the callback function to the switch_locale action
    add_action( 'switch_locale', 'wpdocs_switch_locale_callback', 10, 2 );

You must log in before being able to contribute a note or feedback.