Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasperf/75a930ca14b26bc31f32bf91dce25829 to your computer and use it in GitHub Desktop.
Save jasperf/75a930ca14b26bc31f32bf91dce25829 to your computer and use it in GitHub Desktop.
<?php
/**
* Add login and logout links to main menu
*
* @param string $items The HTML list content for the menu items.
* @param obj $args An object containing wp_nav_menu() arguments.
*
* @link https://codex.wordpress.org/Function_Reference/wp_login_url
* @link https://codex.wordpress.org/Function_Reference/wp_logout_url
* @link https://developer.wordpress.org/reference/functions/wp_nav_menu/
* @link https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/
*/
function frontend_login_logout_menu_items( $items, $args ) {
if ($args->theme_location == 'primary') {
if ( is_user_logged_in() ) {
$items .= '<li class="menu-item"><a href="' . wp_logout_url( home_url() ) . '">Logout</a></li>';
} else {
$items .= '<li class="menu-item"><a href="' . wp_login_url( get_permalink() ) . '" title="Login">Login</a></li>';
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'frontend_login_logout_menu_items', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment