How to add php in WordPress Menu

I’d like to add PHP code to some links of my WordPress menu. I use

Dashboard > Appearance > Menus

to make up the menu. If I add PHP code there, it doesn’t work.

Read More

I need the menu links like:

<a href="http://mcadrives.com/?ref=<?PHP code goes here?>">

If I want that querystring to be present on every menu item I have based through a custom walker, where exactly should I add my code?

The php coding I have been using on every hyperlink and image is this:

<a href="http://mcadrives.com/?ref=
    <?php
       if (!empty ( $_SERVER['QUERY_STRING'])){
         echo substr($_SERVER['QUERY_STRING'],4); }
       else{ 
         echo 'mhammonds'; }
    ?>>

I also know I would add this to my functions.php file and then call the walker in my wp_nav_menu, but where and how do I exactly add my coding?

    class Query_Nav extends Walker_Nav_Menu
{
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $value . $class_names .'>';

        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';

        //ADD YOUR PHP HERE TO DETERMINE WHATEVER IT IS YOU NEED FOR YOUR LINK
        $addedStuff = 'some added stuff to append to your URL';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url.$addedStuff) .'"' : '';
        ////////////////////

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

Related posts

1 comment

  1. In your Query_Nav walker class, change this line:

    $addedStuff = 'some added stuff to append to your URL';
    

    to these lines:

    if ( ! empty ( $_SERVER['QUERY_STRING'] ) ) {
         $addedStuff = '?ref=' . substr( $_SERVER['QUERY_STRING'], 4 );
    }else { 
         $addedStuff = '?ref=mhammonds';
    }
    

    And you’re done… just make sure the call to wp_nav_menu is actually using that walker.

Comments are closed.