Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

We could try to filter the WPLANG option locale (see e.g. this approach from the related list here on the right(see e.g. this approach from the related list here on the right by @brasofilo, that's based on this onethis one by @toscho ):

Here's an exampleexample by @sanchothefat on how we can add custom user settings.

We could try to filter the WPLANG option locale (see e.g. this approach from the related list here on the right by @brasofilo, that's based on this one by @toscho ):

Here's an example by @sanchothefat on how we can add custom user settings.

We could try to filter the WPLANG option locale (see e.g. this approach from the related list here on the right by @brasofilo, that's based on this one by @toscho ):

Here's an example by @sanchothefat on how we can add custom user settings.

added 164 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
/**
 * Display available language dropdown
 */
function wpse_user_language( $user ) 
{
    // Only display for non-admins, but allow admins to edit for other users
    if( current_user_can( 'manage_options' ) && $user->ID == get_current_user_id() )
        return;

    // Get the current 'wp_lang' settings
    $lang = get_user_meta( $user->ID, 'wpse_lang', true ); 
    ?>
    <table class="form-table">
        <tr id="wpse-lang-selection">
            <th scope="row">
               <label for="wpse_lang">
                   <?php _e( 'WPSE' ); ?> - <?php _e( 'Site Language' ); ?>
               </label>
            </th>
            <td><?php wp_dropdown_languages( 
                    [
                        'id'                          => 'wpse_lang',
                        'name'                        => 'wpse_lang',
                        'languages'                   => get_available_languages(),
                        'translations'                => [],
                        'selected'                    => $lang,
                        'show_available_translations' => false,
                    ] 
                );
            ?></td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'wpse_user_language' );
add_action( 'edit_user_profile', 'wpse_user_language' );

Hopefully you can adjust this to your needs. It would be a good idea to wrap this in a class, where we could initialize it with a custom setup and re-use things for better performance. We might also consider removing the is_admin() check to apply this also on the front-end, but then we might need an extra is_user_logged_in() check.

/**
 * Display available language dropdown
 */
function wpse_user_language( $user ) 
{
    // Only display for non-admins
    if( current_user_can( 'manage_options' ) )
        return;

    // Get the current 'wp_lang' settings
    $lang = get_user_meta( $user->ID, 'wpse_lang', true ); 
    ?>
    <table class="form-table">
        <tr id="wpse-lang-selection">
            <th scope="row">
               <label for="wpse_lang">
                   <?php _e( 'WPSE' ); ?> - <?php _e( 'Site Language' ); ?>
               </label>
            </th>
            <td><?php wp_dropdown_languages( 
                    [
                        'id'                          => 'wpse_lang',
                        'name'                        => 'wpse_lang',
                        'languages'                   => get_available_languages(),
                        'translations'                => [],
                        'selected'                    => $lang,
                        'show_available_translations' => false,
                    ] 
                );
            ?></td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'wpse_user_language' );
add_action( 'edit_user_profile', 'wpse_user_language' );

Hopefully you can adjust this to your needs. It would be a good idea to wrap this in a class, where we could initialize it with a custom setup and re-use things. We might also consider removing the is_admin() check to apply this also on the front-end.

/**
 * Display available language dropdown
 */
function wpse_user_language( $user ) 
{
    // Only display for non-admins, but allow admins to edit for other users
    if( current_user_can( 'manage_options' ) && $user->ID == get_current_user_id() )
        return;

    // Get the current 'wp_lang' settings
    $lang = get_user_meta( $user->ID, 'wpse_lang', true ); 
    ?>
    <table class="form-table">
        <tr id="wpse-lang-selection">
            <th scope="row">
               <label for="wpse_lang">
                   <?php _e( 'WPSE' ); ?> - <?php _e( 'Site Language' ); ?>
               </label>
            </th>
            <td><?php wp_dropdown_languages( 
                    [
                        'id'                          => 'wpse_lang',
                        'name'                        => 'wpse_lang',
                        'languages'                   => get_available_languages(),
                        'translations'                => [],
                        'selected'                    => $lang,
                        'show_available_translations' => false,
                    ] 
                );
            ?></td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'wpse_user_language' );
add_action( 'edit_user_profile', 'wpse_user_language' );

Hopefully you can adjust this to your needs. It would be a good idea to wrap this in a class, where we could initialize it with a custom setup and re-use things for better performance. We might also consider removing the is_admin() check to apply this also on the front-end, but then we might need an extra is_user_logged_in() check.

added 1141 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
/**
 * Override locale settings for the current (non-admin) user
 */
is_admin() && add_filter( 'locale', function( $locale )
{
    // Modify locale for non-admins (we don't want to override this on the settings pages)
    if( ! current_user_can( 'manage_options' ) )
    {
        // Get admin language for the current user
        $lang = get_user_meta( get_current_user_id(), 'wpse_lang', true );
 )       
        $locale=// Use 'en_US' as default
        $locale = ! empty( $lang ) ? sanitize_text_field( $lang ); : 'en_US';
    }   
    return $locale; 
} );    
/**
 * Display available language dropdown
 */
function wpse_user_language( $user ) 
{
    // Only display for non-admins
    if( current_user_can( 'manage_options' ) )
        return;

    // Get the current 'wp_lang' settings
    $lang = get_user_meta( $user->ID, 'wpse_lang', true ); 
    ?>
    <table class="form-table">
        <tr><tr id="wpse-lang-selection">
            <th><<th scope="row">
               <label for="wpse_lang">
                   <?php _e( 'Admin'WPSE' ); ?> - <?php _e( 'Site Language' ); ?></th>>
            <td>
   </label>
         <?php
    </th>
            <td><?php wp_dropdown_languages( 
                    [
                        'id'                          => 'wpse_lang',
                        'name'                        => 'wpse_lang',
                        'languages'    => get_available_languages(),
              => get_available_languages(),
         'translations' => [],
             'translations'           'selected'     => $lang[],
                        'echo''selected'                    => 1$lang,
                        'show_available_translations' => false,
                    ] 
                );
            ?>
            <></td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'wpse_user_language' );
add_action( 'edit_user_profile', 'wpse_user_language' );

andNote that here we can save it withre-use the Site Language string, because it's translated.

The update part is:

/**
 * Update the 'wp_lang' user settings
 */    
function wpse_user_language_save( $user_id ) 
{
    if( current_user_can( 'edit_user', $user_id ) && isset( $_POST['wpse_lang'] ) )
        return update_user_meta( $user_id, 'wpse_lang', $_POST['wpse_lang'] );
    return false;
}
add_action( 'personal_options_update', 'wpse_user_language_save' );
add_action( 'edit_user_profile_update', 'wpse_user_language_save' );

wpse_lang selectionwpse_lang selection

Hopefully you can adjust this to your needs. It would be a good idea to wrap this in a class, where we could initialize it with a custom setup and re-use things. We might also consider removing the is_admin() check to apply this also on the front-end.

is_admin() && add_filter( 'locale', function( $locale )
{
    if( $lang = get_user_meta( get_current_user_id(), 'wpse_lang', true ) )
        $locale= sanitize_text_field( $lang );
    return $locale; 
} );    
function wpse_user_language( $user ) 
{
    $lang = get_user_meta( $user->ID, 'wpse_lang', true )
    ?>
    <table class="form-table">
        <tr>
            <th><?php _e( 'Admin Language' ); ?></th>
            <td>
            <?php
                 wp_dropdown_languages( 
                    [
                        'id'           => 'wpse_lang',
                        'name'         => 'wpse_lang',
                        'languages'    => get_available_languages(),
                        'translations' => [],
                        'selected'     => $lang,
                        'echo'         => 1,
                        'show_available_translations' => false,
                    ] 
                );
            ?>
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'wpse_user_language' );
add_action( 'edit_user_profile', 'wpse_user_language' );

and we can save it with:

function wpse_user_language_save( $user_id ) 
{
    if( current_user_can( 'edit_user', $user_id ) && isset( $_POST['wpse_lang'] ) )
        return update_user_meta( $user_id, 'wpse_lang', $_POST['wpse_lang'] );
    return false;
}
add_action( 'personal_options_update', 'wpse_user_language_save' );
add_action( 'edit_user_profile_update', 'wpse_user_language_save' );

wpse_lang selection

/**
 * Override locale settings for the current (non-admin) user
 */
is_admin() && add_filter( 'locale', function( $locale )
{
    // Modify locale for non-admins (we don't want to override this on the settings pages)
    if( ! current_user_can( 'manage_options' ) )
    {
        // Get admin language for the current user
        $lang = get_user_meta( get_current_user_id(), 'wpse_lang', true );
        
        // Use 'en_US' as default
        $locale = ! empty( $lang ) ? sanitize_text_field( $lang ) : 'en_US';
    }   
    return $locale; 
} );    
/**
 * Display available language dropdown
 */
function wpse_user_language( $user ) 
{
    // Only display for non-admins
    if( current_user_can( 'manage_options' ) )
        return;

    // Get the current 'wp_lang' settings
    $lang = get_user_meta( $user->ID, 'wpse_lang', true ); 
    ?>
    <table class="form-table">
        <tr id="wpse-lang-selection">
            <th scope="row">
               <label for="wpse_lang">
                   <?php _e( 'WPSE' ); ?> - <?php _e( 'Site Language' ); ?>
               </label>
            </th>
            <td><?php wp_dropdown_languages( 
                    [
                        'id'                          => 'wpse_lang',
                        'name'                        => 'wpse_lang',
                        'languages'                   => get_available_languages(),
                        'translations'                => [],
                        'selected'                    => $lang,
                        'show_available_translations' => false,
                    ] 
                );
            ?></td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'wpse_user_language' );
add_action( 'edit_user_profile', 'wpse_user_language' );

Note that here we re-use the Site Language string, because it's translated.

The update part is:

/**
 * Update the 'wp_lang' user settings
 */    
function wpse_user_language_save( $user_id ) 
{
    if( current_user_can( 'edit_user', $user_id ) && isset( $_POST['wpse_lang'] ) )
        return update_user_meta( $user_id, 'wpse_lang', $_POST['wpse_lang'] );
    return false;
}
add_action( 'personal_options_update', 'wpse_user_language_save' );
add_action( 'edit_user_profile_update', 'wpse_user_language_save' );

wpse_lang selection

Hopefully you can adjust this to your needs. It would be a good idea to wrap this in a class, where we could initialize it with a custom setup and re-use things. We might also consider removing the is_admin() check to apply this also on the front-end.

added 1679 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading
added 1679 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading
added 75 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading
added 164 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading
added 100 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading