How to add a shortcode for custom menu item in WordPress


Hi Guys,

Today i will explain how to use short code for custom menu item in navigation.

I have tried and got the result for it. Here i explained how i did it.

First you have to install the this plugin for url short-code -> http://wordpress.org/extend/plugins/peters-blog-url-shortcodes/

Add this code to your functions.php file with your theme.


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

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;

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

$output .= $indent . '

  • 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 ) .'"' : '';

    //echo $item->url;
    $string = explode('::', $item->url, 3);
    if($string[1]){
    $string[1] = str_replace('-',' ', $string[1] );
    $item->url = do_shortcode("[$string[1]]");
    }

    $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

    $prepend = '';
    $append = '
    ';
    $description = ! empty( $item->description ) ? ''.esc_attr( $item->description ).'' : '';

    if($depth != 0)
    {
    $description = $append = $prepend = "";
    }

    $item_output = $args->before;
    $item_output .= '';
    $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
    $item_output .= $description.$args->link_after;
    $item_output .= '
    ';
    $item_output .= $args->after;

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

    Then you have to call this function from wp_nav_menu from the templates files.


    $arg = array( 'menu' => "main-menu", 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'depth' => 0, 'walker' => new description_walker() );

    wp_nav_menu( $arg );

    Thats it. Then go to the back-end with menu section.

    For example i want to give the page URL to custom link.

    I will add like this —–> http://::blogurl-id=’1302′::

    That’s All.

    Now you can go to the front-end and check the shortcode will work.

    Thanks.

    About these ads
  • 8 thoughts on “How to add a shortcode for custom menu item in WordPress

    1. I have a Genesis child theme. Where would I add this more specifically? “you have to call this function from wp_nav_menu from the templates files”?

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s