Support » Fixing WordPress » How to change href lang on single pages/posts?

  • ralphmuc

    (@ralphmuc)


    Hi all,
    can anyone help me out with a few php-lines for my functions.php (or where it´s needed) to change e.g:
    html lang=”de-DE” into html lang=”de-AT”
    or
    html lang=”de-DE” into html lang=”cs-CZ”

    The page/post content itself I´ll publish in the pre-translated language – and only on 3-4 static pages/posts.
    So I don´t need any other things (like changing flags, admin/backend functionality…). It´s just for SEO to show the correct lang/country the page/post is pointing to.

    Thanks Ralph

    • This topic was modified 3 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 11 replies - 1 through 11 (of 11 total)
  • Anton Vlasenko

    (@antonvlasenko)

    Hi,
    You could add something like this to your .php file:

    add_filter('locale', 'change_page_locale');
    function change_page_locale( $default_locale )
    {
      $pages_to_change_locales_for = [
        'my-page-url-slug-1' => 'de-AT',
        'my-page-url-slug-2' => 'de-AT',
        'my-page-url-slug-3' => 'cs-CZ',
        'my-page-url-slug-4' => 'cs-CZ',
      ];
    
      foreach ($pages_to_change_locales_for as $slug => $locale) {
       if (is_page($slug)) {
          return $locale;
          }
       }
       return $default_locale;
    }

    `
    Replace my-page-url-slug-X with the actual URL slug of you the page you want to change the locale for.
    Replace de-AT with the locale you’d like to use for that page.
    Please let me know if it works.

    Thread Starter ralphmuc

    (@ralphmuc)

    Hi Anton,
    thanks for your support. But sorry, it doesn´t work 🙁

    The my-page-url-slug-X I replaced with (e.g.)
    https://www.my-siteurl.com/my-post-or-page-url/

    I inserted your code at the end of the theme functions.php

    Best Ralph

    • This reply was modified 3 months ago by ralphmuc.
    • This reply was modified 3 months ago by ralphmuc.
    • This reply was modified 3 months ago by ralphmuc.
    Anton Vlasenko

    (@antonvlasenko)

    Hi @ralphmuc,
    I’m not sure if I understand you correctly, but you don’t have to use whole URL as a URL slug.
    You just need to use the part of the URL after the server name, e.g. “my-post-or-page-url”.

    For the example that you provided, you need to use the following code:

    add_filter('locale', 'change_page_locale');
    function change_page_locale( $default_locale )
    {
      $pages_to_change_locales_for = [
        'my-post-or-page-url' => 'de-AT',
      ];
    
      foreach ($pages_to_change_locales_for as $slug => $locale) {
       if (is_page($slug)) {
          return $locale;
          }
       }
       return $default_locale;
    }

    If you tell me your WordPress version I can test this snippet on that particular WordPress version to make sure it works.

    Hi @ralphmuc,

    You can try either of these methods below:

    Method 1: Add hreflang tag in WordPress using the multilingual plugin – Polylang. It is a free WordPress plugin and comes with a very powerful and easy-to-use interface for managing multilingual content.

    Method 2: Add the hreflang tag in WordPress without using a multilingual plugin The first thing you need to do is install and activate the HREFLANG Tags Lite plugin. It will add a new menu item labeled hreflang in your WordPress admin menu.

    Clicking on the plug-in and it will redirect you to the setting page. Select the post type for which you want to enable the plugin and then click the save changes button to save your settings. Next, you need to edit the post or page where you want to add the hreflang tag. On the post edit screen, you’ll see a new meta box labeled hreflang tags.`

    Let me know if my answer can help you resolve your issue.

    Thread Starter ralphmuc

    (@ralphmuc)

    Hi @antonvlasenko
    I´m using WP 5.7.2. (german version with template twentyseventeen)
    And also the test just with (example) the part “my-post-or-page-url” didn´t help. Thx for your efforts!

    @lucaslitextension

    “Let me know if my answer can help you resolve your issue.”
    No, I don´t use multi-lang pages/posts that have to be switched over or the same pages in two different languages. Just need it as described in TO.

    • This reply was modified 2 months, 4 weeks ago by ralphmuc.

    @ralphmuc I’ve just realised that my code will not work for posts, it only works for pages.
    I’ve updated it so now it should work for both posts and pages.

    add_filter('locale', 'change_page_locale');
    function change_page_locale( $default_locale )
    {
      $pages_to_change_locales_for = [
        'my-post-or-page-url' => 'de-AT',
      ];
    
      foreach ($pages_to_change_locales_for as $slug => $locale) {
       if (is_page($slug) || ($slug === get_post_field('post_name'))) {
          return $locale;
          }
       }
       return $default_locale;
    }

    Of course, you will have to replace my-post-or-page-url with the actual slug of your page or post.
    Please let me know if it works.
    I’ve tested it on WP 5.7.2 and twenty seventeen theme and it should be working.
    Screenshot 2021 07 28 at 19 23 07

    Thread Starter ralphmuc

    (@ralphmuc)

    @antonvlasenko
    Sorry, I was sick for a few days…
    And one more time – sorry, it does not work. Tried with post & page without effect.

    Maybe I include your code to the wrong php file?
    Where do you include it?

    Hi, @ralphmuc.
    I’d try to include it into
    wp-content/themes/twentyseventeen/functions.php
    file (in case you use twentyseventeen theme).
    Please let me know if it works.

    Thread Starter ralphmuc

    (@ralphmuc)

    Hi @antonvlasenko
    That´s exact the same file I use. I added the code at the end of functions.php using the theme editor in WP Admin/Backend.
    Is it possible, that
    – there are differences between the german<>english version of the theme?
    – the new function is overwritten by something after the call of “functions.php”?

    Best Ralph

    @ralphmuc

    there are differences between the german<>english version of the theme?

    I don’t think there is any difference in terms of how the lang attribute works (there must be no difference).

    the new function is overwritten by something after the call of “functions.php”?

    It’s hard to tell without having access to your WordPress installation. You can try to disable all plugins and check if it resolves the issue.
    Please bear in mind that you need to modify the code of the function I posted and change my-post-or-page-url to the actual URL slug of your page or post.

    Thread Starter ralphmuc

    (@ralphmuc)

    @antonvlasenko

    I´m really frustrated. All your (an my) efforts have no success…
    – Tried to deactivate all plugins
    – Tried on an other site & WP insatllation (same ver & theme)

    Nothing!
    The coming days I´ll install a new WP (using a parked domain) to try this. I´ve no more ideas…

    Best Ralph

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.