Append Items to A WordPress Nav Menu

WordPress provides a filter called wp_nav_menu_items which allows you to easily append your own list items to a menu. This allows you to do cool things like automatically append an ‘Admin’ link to the end of the footer menu. This is particularly useful when handing off a site to a client and you want to ensure there is always a link to the admin area that the client can’t accidentally delete.

As you can see, I’m doing a conditional check for the specific menu I’m wanting to edit, so you would need to adapt this to your specific use case. You would also want to edit the function name to add a custom prefix and add your plugin or theme’s text domain to the __() translation function.

Other great use cases for using this filter could be:

  • Showing a ‘Login’ or ‘Logout’ link conditionally, depending on whether the user is logged in.
  • Showing a ‘Profile’ link, only for logged in users.
  • Adding a list item containing a search field.

If you are wanting to use this in a publicly released theme or plugin, just be sure to take into account that someone may use wp_nav_menu() and provide custom values for the before, after, link_before and/or link_after parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.