1

I want to add a variable in the Menu, for passing a value in all my page. The purpose is to retrieve a value for transport to another web site (from my website links) to the tracking, this value depends on the origin of the user.

eg of my menu :

- Home = mylink/?partenaire=<?php
echo $myPartenaire;
?>

- About us = mylink/aboutus.php?partenaire=<?php
echo $myPartenaire;
?>

- Contact = mylink/contact.php?partenaire=<?php
echo $myPartenaire;
?>

I can retrieve the value in my page but not in the menu

  1. I've installed this plugin to put PHP in my page http://wordpress.org/extend/plugins/insert-php/

  2. In function.php before "?>" at the bottom of the page, I've add :

    $myPartenaire =  $_GET['partenaire'];
    
  3. In header.php At the beginning, I've add :

    <?php global $myPartenaire ;  ?>
    
  4. In my page I can retrieve the value, I've add :

    [insert_php] global $myPartenaire ;  [/insert_php]
    
    <a href="http://mylink.com/mypage.php?partenaire=[insert_php]
    echo $myPartenaire;
    [/insert_php]">LINK OF MY PAGE</a>
    
    eg : mylink/contact?partenaire=<?php
    echo $myPartenaire;
    ?>
    

I know after how to get the value of "partenaire" in my page, but I don't know how to add the value

partenaire=<?php
echo $myPartenaire;
?>

in all the link in my nav menu.

1

You should never need a plugin to insert raw PHP code somewhere. There are always side effects you cannot see or fix easily.

Filter wp_nav_menu_objects instead, and add the parameter here to the URLs.

if ( ! empty ( $_GET['partenaire'] ) )
    add_filter( 'wp_nav_menu_objects', 'wpse_82194_add_param' );

/**
 * Add a parameter to item URLs.
 *
 * @wp-hook wp_nav_menu_objects
 * @param   array $items
 * @return  array
 */
function wpse_82194_add_param( $items )
{
    $out = array ();

    foreach ( $items as $item )
    {
        $item->url = add_query_arg( 'partenaire', $_GET['partenaire'], $item->url );
        $out[] = $item;
    }

    return $items;
}
  • Thanks a lot toscho, it works perfectly, but I've just noticed that when I click on the logo of my website, I lose the variable in the URL. I you click on disneylive-spectacle.fr/?partenaire=disneystore and after you click on the logo Disney Live, you lose the tracking, but if you click on "Accueil" = "Home", you keep the tracking. Is it possible to keep the tracking when I click on the logo, if not, can you tell me how to make the link on the logo desactivated? Thanks a lot. – Pomme Prod Jan 21 '13 at 13:03
  • Is the logo a nav menu item? – fuxia Jan 21 '13 at 13:05
  • Yes, it's the logo nav menu. – Pomme Prod Jan 21 '13 at 14:10
  • No, the logo is not part of the nav menu. You will have to add the parameter separately there. – fuxia Jan 21 '13 at 14:32
  • Thanks toscho, can you tell me how to modificate the parameters for the logo in the nav menu? I don't find the right file to do that. Thanks a lot. – Pomme Prod Jan 21 '13 at 18:52
0

See this answer on creating a custom menu Walker https://wordpress.stackexchange.com/a/14039/6477

Then instead of adding a "description" as per the answer, you should be able to modify the href link.

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.