[resolved] Adding URL parameters to menu items (3 posts)

  1. hydrurga
    Posted 2 years ago #

    I currently use 2 menus on my site, defined through Appearance->Menus, each menu written in a different language. I use my own PHP code to distinguish between the 2 when generating a page:

    <?php switch($_SESSION['lang']) {
     case 0: wp_nav_menu(array('theme_location' => 'primary')); break;
     case 1: wp_nav_menu(array('theme_location' => 'primary', 'menu' => 'French Menu'));
    } ?>

    The French pages can be obtained by e.g.


    The above works well but the principal question is this: does anyone know of a way, without hard-coding WP core, that I can have the French menu automatically append an URL parameter on to the end of all of its menu items?

  2. hydrurga
    Posted 2 years ago #

    For further info, the hard-coding of WP core that I would like to avoid, but which I am currently forced to use, is the changing of line 86 of wp-includes\nav-menu-template.php


    $attributes .= ! empty($item->url) ?(' href="'.esc_attr($item->url).(($_SESSION['lang']==1)?'?language=1':'').'"'):'';

    using the php session variable that I set up depending on the current language choice.

  3. hydrurga
    Posted 2 years ago #

    In the end, I decided to create a series of custom links in the French menu, specifying URLs containing the ?language=1 parameter. A bit of work involved, but it prevents issues arising from directly editing the WP core. Marking as resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic