How to Add Custom Items to WordPress Menus

How to Add Custom Items to WordPress Menus

WordPress menu really a awesome way to drag and drop items. One thing that is limited on the visual interface of menus is that you can only add links (pages, category, or custom links). What if you want to add a custom item to WordPress menus? Maybe you want to add a search bar, or log in/out link, today’s date, or anything else in a WordPress menu. Just because there is no visual interface, does not mean it is not possible. In this article, we will show you how you can utilize the wp_nav_menu_items  hook to add custom items to all or specific WordPress menus.

Note: before start working you should enough knowledge about wordpress, so it is expected that you know basic html/css and a fair understanding of how WordPress themes work.

Obviously, you need to have custom menu enabled in your themes before you can proceed any further.

Lets start with the basics. We need to add our own filter into
wp_nav_menu_items  hook. An example would look like this:

The above menu only show in single page but if you wan to show all pages just change it  is_single()  or any specific page  is_single(123)  or only home page  is_home()

To create a hierarchy (dropdown) of menu items, you could write your hook like this:

Be careful. It’s important that no menu items share the same menu_order property.

You can use the conditional statements along with the argument theme_location. This allows you to target a specific menu location with any condition you want. If you don’t want the conditional statement, you don’t have to use it. Just add it to a specific menu location or vice versa.

Now that you have seen a basic example, let us show you some specific examples of how this would work.

Adding Log in/out links to a Specific WordPress Menu

If you want to give your users the ability to log in/out, then one place you can add the links is in your custom menu. The snippet below will show log in/out links to your users appropriately on the menu location: primary. You can change the menu location if you want.

Adding a Search Bar to a Specific Menu

Want to add a search bar to a specific menu? Well look no further. You can do so by pasting the following snippet:

I hope this tutorial help you. if you stack please write a comment below.

Md. Elias
elias1435@gmail.com
2 Comments
  • Linyah
    Posted at 21:33h, 08 March Reply

    Thank you, Meagan!

Post A Comment