How can I Add a variable PHP in the Menu Nav

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 :

Read More
- 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.

Related posts

Leave a Reply

2 comments

  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;
    }