Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
WordPress 5 Cookbook

You're reading from   WordPress 5 Cookbook Actionable solutions to common problems when building websites with WordPress

Arrow left icon
Product type Paperback
Published in Mar 2020
Publisher Packt
ISBN-13 9781838986506
Length 660 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rakhitha Nimesh Ratnayake Rakhitha Nimesh Ratnayake
Author Profile Icon Rakhitha Nimesh Ratnayake
Rakhitha Nimesh Ratnayake
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Setting Up WordPress and Customizing Settings 2. Customizing Theme Design and Layout FREE CHAPTER 3. Using Plugins and Widgets 4. Publishing Site Content with the Gutenberg Editor 5. Managing Users and Permissions 6. Setting up a Blogging and Editorial Workflow 7. WordPress as an Application Framework 8. Improving Usability and Interactivity 9. Building E-Commerce Sites with WooComerce 10. Troubleshooting WordPress 11. Handling Performance and Maintenance 12. Improving Site Security 13. Promoting and Monetizing the Site 14. Other Books You May Enjoy Appendix

Creating conditional navigation menus

We discussed the importance of navigation menus in the previous recipe. The items in the navigation menu are also important in modern sites due to the dynamic nature of their content. Modern sites like to involve users in site functionality rather than providing static content. A high percentage of sites provide different content for different users or user types. So, the menu is no longer a static one. We need to load user-specific menu items or menus to allow access only to the content for each user or user type.

In this recipe, we are going to create multiple navigation menus and display them conditionally based on the user role.

Getting ready

Open the code editor and make sure you have access to the theme files in your local or external WordPress installation.

How to do it...

We have to create multiple navigation menus. Follow these steps to create menus before loading them conditionally inside the theme:

  1. Log in to the WordPress Dashboard as an administrator.
  2. Click the Appearance menu.
  3. Click the Menus option.
  4. Click the create a new menu link.
  5. Add a unique name to the menu. In this case, we're using WPCookbookMenu1.
  6. Click the Create Menu button.
  7. Add items to the menu from Posts, Pages, Custom Links, and Categories by selecting them and clicking the Add to Menu button, as shown in the following screenshot:
  1. Click the Save Menu button to save the menu.
  2. Follow the same process from steps 4 to 7 and create two menus with different menu items and menu names.

Now, you should have three menus listed inside the Select a menu to edit drop-down field. Follow these steps to display the menus conditionally to different user roles:

  1. Go to the twentytwenty theme folder using the file manager and copy header.php file
  2. Go to twentytwentychildtheme folder and paste the header.php file
  3. Open the wp-content/themes/twentytwentychild/header.php file with your code editor.
  4. Find the following code block, which is used for displaying the default menu:
wp_nav_menu(
array(
'container' => '',
'items_wrap' => '%3$s',
'theme_location' => 'primary' ) );
  1. Replace it with the following code to dynamically load the menu based on user types:
$roles_menus = array('subscriber' => 'WPCookbookMenu1', 'administrator' => 'WPCookbookMenu2') ;
$menu_name = 'WPCookbookMenu3';
foreach ($roles_menus as $key => $menu) {
if(current_user_can($key)){
$menu_name = $menu;
}
}

wp_nav_menu(
array(
'menu' => $menu_name,
'container' => '',
'items_wrap' => '%3$s',
'theme_location' => 'primary' ) );
  1. Save the changes to the file.

Now, you need to view the home page as an administrator or subscriber as a normal user. If you only have the admin user on the site, use the Users section in the backend to create a new user with the Subscribe role for testing. You will see different menus displayed for each of the three user types.

How it works...

We can create an unlimited number of menus in the WordPress menu section and assign them to different menu locations in the theme. In this scenario, we wanted to change the top menu for different user types and show different menu items. So, we created three menus called WPCookbookMenu1, WPCookbookMenu2, and WPCookbookMenu3.

The menu of the Twenty Twenty theme is loaded from the header.php template. So, we copied the template into the TwentyTwenty Child theme folder with the same path. Then, we removed the existing menu generation code and added custom code to include the conditional checks.

In the first part of the code, we created an array to store user roles and a menu name to display the respective user role. Next, we used a foreach loop to traverse through the array and check the permission level of the current user with the current_user_can function. Once a match was found, we changed $menu_name to a role-specific menu while keeping WPCookbookMenu3 as the default value for $menu_name. The last part of the code is the same as the original code in the Twenty Nineteen theme, except for the addition of the 'menu' => $menu_name parameter and the removal of the theme_location parameter from the wp_nav_menu function.

The menu name changes based on the user role, which is used to conditionally display the menu. When a matching user role is not found, it will display the WPCookbookMenu3 menu for guests and logged in users with other roles.

Before moving on to the next recipe, replace the header.php file inside the child theme folder with the original file from the Twenty Twenty theme. We're doing this since we want to use the original theme for the next recipe.
You have been reading a chapter from
WordPress 5 Cookbook
Published in: Mar 2020
Publisher: Packt
ISBN-13: 9781838986506
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime