Swap menus in WordPress without a plugin

How to display a different menu in the same location as your primary menu based on page, post, template, category, or your site’s forum.

WordPress menus by design allow a unique menu to inhabit a particular location on your site. For example, the primary navigation menu is uniform across all pages with other menus possibly existing in other locations like the very top of your site, in a sidebar as a widget, or within the site’s footer as a widget. Out of the box however, WordPress does not allow selection of a different menu in the same location depending on the page you are on.

There are good reasons to show a different menu. Your site may have a members area that gives access to private areas of your site, and so you may want to have a different menu. You might want a different menu if you are a commercial client as opposed to an individual client. Or perhaps you want the public facing part of your site to have a different menu to what people in your organisation see. Whatever your reasons, read on to learn to accomplish this easy task. You do not even need to be a programmer as we will provide a good sample code for you.

Step 1 – Create the menus

Create all menus. You do this by going to Appearance > Menus. Click ‘Create Menu’ and select the pages, posts, categories, custom links, etc and then click ‘Add to Menu’. You can then add further links and you can drag and drop these links in the order you desire and also place links underneath others to that they will appear in a sub-menu. Your default menu should also be set in the ‘Primary Menu’ location. The menu or menus you wish to swap do not need to be assigned a location. In fact you cannot assign two menus to the same location using WordPress anyway.

Step 2 – Find out the ID of your alternate menu

Setup your alternative menu and find out what the ID of that menu is. You can do this by choosing a menu next to the ‘ Select a menu to edit:’ heading. Now take a look at the URL, you will see something like:
“https://wordpresssite.com/wp-admin/nav-menus.php?action=edit&menu=50 “. See the ’50’ at the end of the URL, that is the ID for the menu. Of course your menu will likely have a different ID.

Step 3 – Implement code in your theme’s function file

First you need to understand the condition you want to load your new menu. Do you want it to load based on a template, a page or pages, a category, or perhaps in your BBPress forum? Below is a sample code that loads a different menu based on a template. So all pages that use this template will load the alternate menu. Feel free to copy and paste this into your theme’s function file perhaps at the bottom of the file and then edit.

add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
function bb_wp_nav_menu_args( $args ) {
// change the menu in the Header menu position
if( $args['theme_location'] == 'primary' && is_page_template('members.php') ) {
$args['menu'] = '20'; // 20 is the ID of the menu we want to use here
}
return $args;
}

Obviously your menu ID will probably be different and the condition as to why you want the menu to show will likely be different too. If you are happy to show a different menu based on a template you can reference an already existing template in your theme or create a new one by copying the site’s main template or other template and add it inside your child theme, Rename it to whatever makes sense to you. The below conditional references a template called ‘members.php’. You might want to call your template ‘english.php’ or ‘spanish.php’ if you wanted to display a different menu based on language, so you would swap out the reference to members.php to say spanish.php. So every page written in Spanish would then use the spanish.php template and the main menu would be in Spanish. Whatever your reason, just use a good naming scheme so it is easy for you to follow.

So, using the code above, you would change the below snippet from

if( $args['theme_location'] == 'primary' && is_page_template('members.php') ) {

to

if( $args['theme_location'] == 'primary' && is_page_template('spanish.php') ) {

That’s it.

Step 4 – Changing the function

This step is only relevant if you wish to swap the menu based on another factor like only certain pages, posts, categories, or on a unique templates like BBPress forum templates. Here is a list of functions that you can swap from the code.

Page

if( $args['theme_location'] == 'header' && is_page('1159') ) {

To find the unique ID (number) for your page, edit the page and find the number in the URL. It will look something like this: post=1159.

BBPress

if( $args['theme_location'] == 'primary' && is_bbpress('') ) {

Multiple Functions

Here is an example for using the menu based on a template and also BBPress which may use a different template. This is the whole code snippet:

add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
function bb_wp_nav_menu_args( $args ) {
// change the menu in the Header menu position
if( $args['theme_location'] == 'primary' && is_page_template('members.php') ) {
$args['menu'] = '20'; // 20 is the ID of the menu we want to use here
}
if( $args['theme_location'] == 'primary' && is_bbpress('') ) {
$args['menu'] = '20'; // 20 is the ID of the menu we want to use here
}
return $args;
}

With the samples I have provided, you should be able to edit your code now for multiple pages, templates, and / or BBPress. Here is a list of other functions that are built into WordPress that you can use to launch your alternate menu from.

Step 5 – Styling the new menu

You can change the colour and style of your menu. By default both your main menu and alternate menu will have the same styling. Here is one way to change this.

 

  1. Install a plugin that applies unique classes to your body tag like this plugin – Ambrosite Body Class Enhanced
    https://wordpress.org/plugins/ambrosite-body-class-enhanced/
  2. Read about this plugin and how it works. In short you are able to target classes based on page, page template, and other useful variables. So in your custom style sheet you can for example target all pages using a particular template and write css to target the menu that use that template. Your CSS could look something like the following. Notice how the members template and BBPress is mentioned.

body.page-template-members .current-menu-item, body.page-template-members .current-menu-parent, body.page-template-members .current-menu-ancestor, body.bbpress .current-menu-item, {
color:#822433 !important;
}

Allowing BBPress members to upload media in forums

BBPress is a great forum and the best forum for WordPress. But it has one very annoying limitation, members cannot Add Media to the forum posts.

If you are running BBPress and host a community that you trust and want to give members the ability to not only post images but to also have the full visual toolbar? Stay tuned.

Before going into the required steps, I did make all this happen on a forum some years ago but in a completely different way. I tried a number of solutions and implemented code that was offered by others to be placed into your theme’s function file. Finally I got it to work, but I honestly can not tell you what is actually making it work since I implemented a number of different solutions. But recently I created a new site for a client and they wanted the same thing. I found the simplest way to make this happen. The steps are below:

  1. Install the bbPress Enable TinyMCE Visual Tab. You can find it here:
    https://wordpress.org/plugins/bbpress-enable-tinymce-visual-tab/
  2. Once the plugin is activated, click on settings where the plugin is listed on the Plugin page and you will be taken to a section on the Forum setting page.
  3. You need to make sure the following options are turned on: ‘Post Formatting’. ‘Fancy editor full’. It is not necessary, but I recommend that you turn on the ‘Auto-embed links’ option for YouTube and Facebook content. Next, turn on ‘Fancy editor media upload’ only if you want members to be able to access and embed content already in the Media Library, otherwise turn that off as they cannot upload media from there, unless you are an Editor or Admin. To allow uploading of new media, you need to install a User Role plugin and allow Subscribers and Participants roles the ability to upload media. This is not as easy as you think. It seems to work randomly and works differently on sites I have worked on perhaps because different hosts have different security. I recommend you don’t bother trying this method and jump to the next step.
  4. Install the ‘Image Upload for BBPress’ plugin found here:
    https://wordpress.org/plugins/image-upload-for-bbpress/

Your done. Now when you post a topic or reply in BBPress, you will see the full Visual toolbar and when you editor is in visual mode, that is, when you click the Visual tab, you will see a new button for uploading media.

Your new Image upload button resides on the end of the toolbar and the button before that will load a new row of buttons on a second row of the toolbar.

Please leave a comment if this article has helped you in any way.

Toggle Button show / hides another row of buttons.

If you have any questions, let me know in the comments below.