• Resolved Rick Curran

    (@rickcurran)


    Hi, I am looking to limit the language switching to only the Posts page, so if a user clicks on another menu item in the main site menu that it will *not* automatically include the language string in the url.

    As an example, if I have a site with a menu structure like “Home”, “About”, “News”, then when a user is on the “News” (Posts) page they can change language to Spanish and the link to “News” would be “/es/news/” but the other main menu links would stay as only “/about/” and not include the language prefix.

    Is there any specific way of limiting the addition of the language string within the plugin? I know I could add some custom code to check whether we are on the main Posts archive page and limit it but it seems like I’d need to start to parse the menu structure output which is starting to get a little more complex than I’d prefer.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author maximeschoeni

    (@maximeschoeni)

    Maybe you can try to just remove the plugin hook that cause home url to be translated, except on posts page :

    add_action('parse_request', function() {
        global $sublanguage;
    
        if (!is_home()) {
    
            remove_action('home_url', array($sublanguage, 'translate_home_url'));
    
        }
    
    }, 11);

    (I did not test this code, please use with caution)

    Thread Starter Rick Curran

    (@rickcurran)

    @maximeschoeni Thanks for the reply! I basically just want all menus in the site to just stay to the default language and never insert “/es/” into any of the urls. So not just links to the homepage but any link generated within a wp_nav_menu. Is this possible?

    Thread Starter Rick Curran

    (@rickcurran)

    @maximeschoeni I have tried the code you’ve suggested and it almost works for what I’m trying to do, however it also removes the language string (e.g. ‘es’) from any get_the_permalink calls within the page. Is there a way to specifically target only the links generated within wp_nav_menu?

    Thread Starter Rick Curran

    (@rickcurran)

    @maximeschoeni Sorry to chase this again, but just to say that I have tried the code example you suggested and it does almost do what I need. The only issue is that it also removes the language string (e.g. ‘es’) from any?get_the_permalink?calls within the page. I was wondering if there was a way to specifically target only the links generated within?wp_nav_menu? Thanks again for your help!

    Plugin Author maximeschoeni

    (@maximeschoeni)

    Ok, so we need to find one wp hook just before nav_menu is generating and one just after, so we can remove then put back our filter. Fortunately wp_nav_menu() function appears to have both. Lets try this:

    
    add_action('parse_request', function() {
        
        global $sublanguage;
    
        if (!isset($sublanguage)) return; // this will prevent an error if ever Sublanguage is deactivated.
    
        if (!is_home()) {
    
           // this hook is triggered before nav menu are generated
            add_filter('pre_wp_nav_menu', function($null) {
    
                global $sublanguage;
    
                remove_filter('home_url', array($sublanguage, 'translate_home_url')); // this is actually not an action but a filter!
    
                return $null;
    
            });
    
            // this one is triggered afterwards: let's put back our filter
            add_filter('wp_nav_menu', function($nav_menu) {
    
                global $sublanguage;
    
                add_filter('home_url', array($sublanguage, 'translate_home_url'), 10, 4);
    
                return $nav_menu;
    
            });
    
        }
    
    }, 11);
    Thread Starter Rick Curran

    (@rickcurran)

    @maximeschoeni Thanks, that seems to be working correctly! The nav menu items are left untouched but get_permalink and also paginate_links links are keeping the language string in them still.

    Thanks again for that code, I’d tried to figure out which action or filter I might be able to use but I didn’t come across that pre_wp_nav_menu filter.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Limit language switching only to the Posts page’ is closed to new replies.