Blog

wordpress add menu item programmatically

 

 

you want to add any link after and before the menu you can do it programmatically by using filter is here.

To do this we use the wp_nav_menu_items filter which will run after WordPress has created the HTML to print the menu on the screen. This gives us access to all the items that will be used, this means that we can simply add a new menu item to the end of the list with the links that we need.

 add_filter( 'wp_nav_menu_items', 'add_logout_link', 10, 2);

/**
* Add a login link to the members navigation
*/
function add_logout_link( $items, $args )
{
if($args->theme_location == 'site_navigation')
{
if(is_user_logged_in())
{
$items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
} else {
$items .= '<li><a href="'. wp_login_url() .'">Log In</a></li>';
}
}

return $items;
}

Leave a Reply