How To Add a Login/Logout Menu Link in WordPress

Simple Link
1 2 3 4 5 6 7 |
$redirect = ( is_home() ) ? false : get_permalink(); if ( is_user_logged_in() ) { $link = '<a href="' . wp_logout_url( $redirect ) . '" title="Log Out">Log Out</a>'; } else { $link = '<a href="' . wp_login_url( $redirect ) . '" title="Log In">Log In</a>'; } echo $link; |
Complex Menu Link
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Add login/logout links to a menu add_filter( 'wp_nav_menu_items', 'add_login_logout_to_menu', 50, 2 ); function add_login_logout_to_menu( $items, $args ){ // change theme location with your them location name $myMenu = 'secondary'; // or primary, etc. if( is_admin() || $args->theme_location != $myMenu ) { return $items; } $redirect = ( is_home() ) ? false : get_permalink(); if ( is_user_logged_in() ) { $link = '<a href="' . wp_logout_url( $redirect ) . '" title="' . __( 'Logout' ) .'">' . __( 'Logout' ) . '</a>'; } else { $link = '<a href="' . wp_login_url( $redirect ) . '" title="' . __( 'Login' ) .'">' . __( 'Login' ) . '</a>'; } $items .= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>'; return $items; } |