5

I have a wordpress site in a language that aligns RTL, but some pages are in English. These pages look terrible, however, because the English text is aligned with the RTL language, and punctuation, etc. appear on the wrong the side.

How can I set the language for a single page or several individual pages?

4 Answers 4

5

The locale filter that allows you to set the locale specifically. You can check the current page, and alter the value based on that.

add_filter('locale', 'change_my_locale');
function change_my_locale( $locale ) {
    if ( is_page('slug-here') ) {
        return 'en_US';
    }
    return $locale;
}
6
  • Which file should I add this code to?
    – Null_Error
    Aug 28, 2017 at 17:06
  • You can create must use plugin with following code and place it in wp-content/mu-plugins folder. codex.wordpress.org/Must_Use_Plugins. Alternative if you can modify theme, you can use functions.php file of your theme. Note that after updating this theme, your custom code will lost. So you need to create child theme to avoid this codex.wordpress.org/Child_Themes Aug 28, 2017 at 17:15
  • I added this to my functions.php, inserting the page slug, and nothing happened.
    – Null_Error
    Aug 28, 2017 at 17:21
  • @Null_Error Try and use true in the conditional, to check if the problem is with the is_page() or the locale itself.
    – Johansson
    Aug 28, 2017 at 17:23
  • You can't use functions.php for it, sorry. Theme loads too late for this hook Aug 28, 2017 at 17:32
4

You should use locale hook to solve your task.

Important note that you can't add it in theme and use is_page function, because it will define later.

So you have to create plugin or must use plugin with url parsing this way:

add_filter('locale', function($locale) {
    $path = trim($_SERVER['REQUEST_URI'], "/");

    // get last part as slug
    $arr = explode("/", $path);
    $slug = end($arr);

    if($slug === 'MY_SLUG')
      return 'ru_RU';

    return $locale;
});

Related answer: Change locale at runtime?

4
  • Well, on doing ome further testing, it appears this code changes the entire website's language, not just that one page.
    – Null_Error
    Aug 29, 2017 at 13:48
  • You right. I've updated my answer and tested it on my test blog installation. It seems everything working. Try it, please Aug 29, 2017 at 13:57
  • I don't understand the term "wrong" here. Your approach is the same, using the locale filter. I didn't test it specifically with the is_page() function, but if it's too soon to use is_page(), then it simply can't be done.
    – Johansson
    Aug 30, 2017 at 6:09
  • I've updated my anwser. Sorry for misunderstanding. Aug 30, 2017 at 10:06
2

I needed something similar for translation & direction purposes.

By defining $direction for each post, 'ltr' or 'rtl'...using this simple plugin ... I edited the plugin by adding following action to plugin.php file:

add_action('wp', function($locale){
    // get post meta from database, which is generated by the plugin
    global $post;
    $direction = get_post_meta($post->ID, 'os_custom_box_for_rtl', true);
    // set locale language based on post meta
    $locale = ($direction == 'rtl')? 'ar':'en_US'; // ...or use your own locale codes
    // if not admin area, do switch! it will work for frontend only per specific page/post
    is_admin() or switch_to_locale( $locale );
});
1

Here is a fix for the problem of timing in the solution by @jack-johansson:

add_action('wp', function() {
  if ( is_page('slug-here') ) {
    add_filter('locale', function( $locale ) {
      return 'en_US';
    });
  }
});
1
  • Unfortunately, this does not work to change the html tag lang attribute if the Site Language is set to something other than the default. Sep 1 at 15:25

Your Answer

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

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