WordPress Development
Automatically Add login or logout Option Item in Menu
This important that some we need to add use login button in font-end specially in menu. In this tune going describe a easy function to add login or logout option in menu item. To add the use login and logout option just take below function in your wordpress theme functions.php file.
add_filter( 'wp_nav_menu_items', 'add_logout_link', 10, 2); /** * Add a login link to the members navigation */ function add_logout_link( $items, $args ) { if($args->theme_location == 'main-menu') { if(is_user_logged_in()) { $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>'; } else { $items .= '<li><a href="'. wp_login_url() .'">Log In</a></li>'; } } return $items; }
Configuration This is very easy to configure in your theme, just here need to change theme_location == 'main-menu' register menu id. Replace main-menu by your menu id, on which you are want add login and logout option. Do not need anything now automatically add login option for non-logged user and show logout for logged user. Now automatically add login and logout option in your given id register menu.
Comments are closed