1

By default in a network install, when viewing a sub-site, the sub-site language is used for the admin bar (toolbar).

How can I have the admin bar (toolbar) always use the language of the user?

6
  • Some quick experimenting shows me that the language of the Admin Bar changes when the user's Language setting (on their Profile page) is set to use a specific language (instead of the Site Default setting). At least, all the items added by Core seemed to change for me.
    – Pat J
    Jun 17, 2021 at 14:53
  • 1
    @PatJ are you sure? There is an unresolved ticket open describing the problem core.trac.wordpress.org/ticket/38643 Jun 17, 2021 at 15:13
  • All I can say for sure is that, once I installed a 2nd language in my local WP installation, the Admin Bar seemed to respect the user's language settings. I didn't dig deep into it, and I don't have a lot of experience with translations — all the sites I deal with are English only.
    – Pat J
    Jun 17, 2021 at 16:08
  • @PatJ thanks. Is it possible your site locale and the user locale are both set to English? That may explain why the user's setting appears to be used. For example English Jun 17, 2021 at 17:36
  • No, the 2nd language I installed was Canadian French, since I wanted something I could still read. When I switched the user's language (in the edit profile) screen to French, the Admin Bar core items were rendered in French, whether the site's language settings were French or English.
    – Pat J
    Jun 17, 2021 at 17:47

1 Answer 1

1

Until there is a fix for it in Wordpress Core you can use this 'dirty' way:

Switch to user locale before admin bar items are processed:

add_filter( 'admin_bar_menu', 'ws390690_admin_bar_in_user_locale', 10, 1 );
function ws390690_admin_bar_in_user_locale($wp_admin_bar){
    switch_to_locale( get_user_locale() );
    return $wp_admin_bar;
}

Switch back to site locale after admin bar has been rendered:

add_filter( 'wp_after_admin_bar_render', 'ws390690_admin_bar_in_user_locale_back', 10, 2 );
function ws390690_admin_bar_in_user_locale_back(){
    restore_previous_locale();
}

As mentioned by @henrywright, its also in works here https://core.trac.wordpress.org/ticket/38643

1
  • This helped, but I also had to remove the language from /wp-content/languages
    – ggedde
    Jan 10 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.