Last active
July 15, 2016 17:56
-
-
Save briankompanee/819cd1868069eb125d906413136bffea to your computer and use it in GitHub Desktop.
WordPress: Add a login/logout link to any existing menu location.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add login/logout link to naviagation menu | |
* Typically this will go in functions.php. | |
*/ | |
function add_login_out_item_to_menu( $items, $args ){ | |
//change the theme_location menu name to match the location in your theme. | |
if( is_admin() || $args->theme_location != 'primary_navigation' ) | |
return $items; | |
$redirect = ( is_home() ) ? false : get_permalink(); | |
if( is_user_logged_in( ) ) | |
$link = '<a href="' . wp_logout_url( home_url() ) . '" title="' . __( 'Logout' ) .'">' . __( 'Logout' ) . '</a>'; | |
else $link = '<a href="' . wp_login_url( $redirect ) . '" title="' . __( 'Login' ) .'">' . __( 'Login' ) . '</a>'; | |
return $items.= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>'; | |
} | |
add_filter( 'wp_nav_menu_items', 'add_login_out_item_to_menu', 50, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment