4

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?

3

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;
}
  • Which file should I add this code to? – Null_Error Aug 28 '17 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 – Anton Lukin Aug 28 '17 at 17:15
  • I added this to my functions.php, inserting the page slug, and nothing happened. – Null_Error Aug 28 '17 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. – Jack Johansson Aug 28 '17 at 17:23
  • You can't use functions.php for it, sorry. Theme loads too late for this hook – Anton Lukin Aug 28 '17 at 17:32
3

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?

  • 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 '17 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 – Anton Lukin Aug 29 '17 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. – Jack Johansson Aug 30 '17 at 6:09
  • I've updated my anwser. Sorry for misunderstanding. – Anton Lukin Aug 30 '17 at 10:06
0

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';
    });
  }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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