WordPress Tutorial: In simple steps, going to show you how to add Register, Login and Logout links in WordPress Main Menu. The following codes are tested by using WordPress Child Theme.
Why should add register, login and logout links in the WordPress main menu?
- Quick registration link for visitors who doesn’t have enough knowledge of how WordPress works.
- Quick login for registered users
- Quick logout for logged in users
How to add register, login and logout links in the WordPress main menu?
It’s quite easy and simple. A beginner with basic level knowledge of WordPress template files and PHP coding can easily follow these below steps.
Open theme functions.php file (I recommend to use WordPress child theme) and add this below code.
/*========================================== Adding Register, Login & Logout link in WordPress Main Menu ===========================================*/ add_filter( 'wp_nav_menu_items', 'adding_register_login_logout_menu', 20, 2 ); function adding_register_login_logout_menu( $items, $args ){ if ( $args->theme_location != 'primary' ){ // Targeting wordpress main or primary menu return $items; } if ( is_user_logged_in() ){ // This below line of code will display logout url when user loged in $items .= '<li><a href="' . wp_logout_url() . '">' . _('Logout') . '</a></li>'; } else { // This below line of code will display login link when user logout $items .= '<li><a href="' . wp_login_url() . '">' . _('Login') . '</a></l>'; // This line of code will display Registration link for new visitor or user $items .= '<li><a href="' . wp_registration_url() . '">' . _('Register') . '</a></li>'; } return $items; }
Save it and you are done.