Skip to content

Instantly share code, notes, and snippets.

@alinademi
Forked from wpscholar/nav-login-logout.php
Created January 29, 2021 01:39
Show Gist options
  • Save alinademi/876b3e9b5de0614082cfbb6a9c9edeb6 to your computer and use it in GitHub Desktop.
Save alinademi/876b3e9b5de0614082cfbb6a9c9edeb6 to your computer and use it in GitHub Desktop.
Automatically append login and logout links to a specific menu location.
<?php
add_filter(
'wp_nav_menu_items',
function( $items, $args ) {
if ( 'primary_nav' === $args->theme_location ) {
if ( is_user_logged_in() ) {
$items .= '<li><a title="Log Out" href="' . esc_url( wp_logout_url() ) . '">' . __( 'Log Out', 'cobb-realtors' ) . '</a></li>';
} else {
$items .= '<li><a title="Login" href="' . esc_url( wp_login_url() ) . '">' . __( 'Login', 'cobb-realtors' ) . ' </a ></li >';
}
}
return $items;
},
10,
2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment