Home

WordPress Filters to add login and logout links in the header menu

WordPress Filters to add login and logout links in the header menu
Like Tweet Pin it Share Share Email

[myad1] Many developers are developing a website on the website and in a few cases, they need to add Login and Logout links in the header.  Many times developers are using a plugin to fulfill this requirement but as a WordPress developer, I’m using the following function to add the Login and Logout links in the header.

WordPress Login and Logout links in the menu.

  • While doing theme customization, if you need to add Login and Logout links in the header menu then copy the below code and paste it in a functions.php file.
  • If you are using child theme then please place this code in a functions.php file and if you are doing with custom plugin the add it in plugin function files.
  • Change the login URL with your current login page.
  • It will Add automatically Login and Logout links in the menu.
function add_login_logout_navitem($items, $args ) {

 if ( !(is_user_logged_in()) ) {
   $login_item = '<li id="menu-item-login" class="menu-item menu-item-type-post_type menu-item-object-page"><a href="'.site_url().'/wp-login.php">Login</a></li>';
 
 }else {
     $login_item = '<li id="menu-item-logout" class="menu-item menu-item-type-post_type menu-item-object-page">'.wp_loginout($_SERVER['REQUEST_URI'], false).'</li>';
 }
 $items .= $login_item;
 return $items;
}
add_filter('wp_nav_menu_items', 'add_login_logout_navitem', 10, 2);

  • This will add with all the menu you need to check which menu you need.
  • Try to print $items and $args, you will get the menu details and add if condition to display the login and logout link at the particular menu,

[AdSense-B]

Please Note: Manage your class name as per menus.

Feel free to ask any query if you have it. If you like this post then please share it.

 

Thanks for reading our article. What Next?

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

15 − 9 =