How to Create WordPress Menu Shortcode Without Plugin

Creating a WordPress shortcode for a menu is super easy, And I am going to tell you the step-by-step process in detail further in this article.

  1. Create Shortcode For Your Menu
  2. Add Menu name in the shortcode to display menu items
  3. Add menu shortcode in a page, post, widget, or template.

Create WordPress Shortcode For Menu

Add the code snippets given below in your theme’s functions.php file to create a shortcode for the menu. This code will create a shortcode [addmenu] in your WordPress theme.

Basically in the below code, I have just created a shortcode with parameters, and then use those parameters with WordPress wp_nav_menu() function.

I have written a detailed article on creating WordPress shortcodes with parameters, where I have outlined how to pass parameters in shortcode.

//==============================================================
//Shortcode to Display Menus
//==============================================================

function diwp_menu_shortcode($attr){

	$args = shortcode_atts(array(

				'name'  => '',
				'class' => ''

				), $attr);

	return wp_nav_menu( array(
				'menu' 	        => $args['name'],
				'menu_class'    => $args['class']
			));
}
add_shortcode('addmenu', 'diwp_menu_shortcode');


The [addmenu] shortcode requires your menu name as a parameter to display the correct menu. You can add a menu name in this shortcode with the name attribute.

That means you can use [addmenu name="your_menu_name"] shortcode to display menus in your pages, posts, widgets and templates.

Add Menu Name in The Shortcode

Now your shortcode is ready, but it will only work if you will add the menu name to it.

Just go to Appearance > Menus and find the menu name as shown in the image below. Copy that menu name from there and add it to your shortcode.

So your menu shortcode will be something like [addmenu name="Primary Menu"]

You can add a class name for the menu by using attribute class="your-class-name" in the shortcode. Eg: [addmenu name="Primary Menu" class="your-class-name"]

create-menu-shortcode-in-wordpress

Add Menu Shortcode in Posts | Pages | Widgets | Template

You can directly add this [addmenu name="your-menu-name"] menu shortcode to your pages, posts, widgets.

However, if you want to use this within your template file or with PHP, then you can use it with do_shortcode() function as given below

<?php do_shortcode('[addmenu name="your-menu-name"]'); ?>

In the same way, You can also create WordPress shortcodes for images and buttons, I have written an article and outlined the process.

Owner of diveinwp.com, solely responsible for creating helpful, informative content about WordPress, a fulltime WordPress developer with 8+ years of hands-on experience in WordPress design and development.

4 thoughts on “How to Create WordPress Menu Shortcode Without Plugin”

  1. Thank you, Thank you, Thank you, a million times Thank you. This works and is exactly what I needed. You explained it very well so that it is easy to understand.

  2. Hi,
    It works great but displays menu as a list with all the sub menus.
    How can I make the sub menu item to appear after hover or clicking the main item.

    1. Bhuvnesh Shrivastava

      Hi Swapnil,

      To create submenu items to be appear on hover or clicking the main item, you can use CSS or JavaScript to show and hide them on hover of the main item.

Comments are closed.