Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Please take some time to learn how to format your posts, especially the code. I tried editing that and just couldn't make sense of it. wordpress.stackexchange.com/editing-help –  s_ha_dum Jan 18 '13 at 20:33
add comment

2 Answers

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;
}
share|improve this answer
    
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? –  toscho 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. –  toscho 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
add comment

See this answer on creating a custom menu Walker http://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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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