/ Published in: PHP
Adds a Logout link to the end of your Primary Navigation is the user is logged in; else adds a Login link to the end of your Primary Navigation.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
add_filter( 'wp_nav_menu_items', 'add_login_logout_to_primary_menu', 10, 2 ); function add_login_logout_to_primary_menu( $items, $args ) { if( 'primary' === $args->theme_location ) : if (is_user_logged_in()) { $items .= '<li><a href="'.wp_logout_url(home_url()).'">Logout</a></li>'; } else { $items .= '<li><a href="'.wp_login_url().'">Login</a></li>'; } endif; return $items; }