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

Fires when the locale is switched.


Parameters

$locale string
The new locale.
$user_id false|int
User ID for context if available.

Top ↑

Source

File: wp-includes/class-wp-locale-switcher.php. View all references

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


Top ↑

Changelog

Changelog
Version Description
6.2.0 The $user_id parameter was added.
4.7.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Harsh Gajipara

    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.