Append Items to a Nav Menu

One of the things that is common when working with WordPress clients is they have a tendency to forget their login URL.  Appending an ‘Admin’ link to the end of the footer that the user can’t remove is one way to ensure your client can always find the admin without having to give you a call.

/**
* Add a custom link to the end of a specific menu that uses the wp_nav_menu() function
*/
add_filter('wp_nav_menu_items', 'add_admin_link', 10, 2);
function add_admin_link($items, $args){
    if( $args->theme_location == 'footer_menu' ){
        $items = $items . '<li><a title="Admin" href="'. admin_url() .'">' . __( 'Admin' ) . '</a></li>';
    }
    return $items;
}

https://gist.github.com/woodent/1249995

With a few alterations, you can append ( or prepend ) anything to a nav menu.  A few examples would be search fields, dynamically generated ‘Log In’ and ‘Log Out’ links, ‘User Profile’ or ‘User Account’ links, fancy images or icon fonts, and the list goes on…

About

Micah is an Atlanta-based WordPress developer who is committed to continuously improving his understanding of WordPress and sharing his insights with the greater WordPress community.

4 thoughts on “Append Items to a Nav Menu

  1. This is a little silly, as it is just as easy to hard code in a link to wp-admin, or whatever. Still, there is something ‘techie’ feeling about using the themes functions file. I might add this snippit to my Flashy Functionality plugin or the Free Theme I’m working on now?!

    • It’s always good practice to avoid hard coding links as they can cause major update problems down the line.
      It doesn’t take much extra work to use dynamic code, trust me, your client and your future self will be thankful of it!

  2. Not really, if you take functionality out of the scope of look and feel this would quite plausibly be applied in a settings plugin, if the theme is ever changed the functionality still remains, if you hard code into the theme you lose it if the theme is changed! ><

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>