Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Preexisting login to change to logout link in sub menu

Here’s how I managed to set it up for one client. The code (which should go into the theme’s functions.php) assumes there’s an existing menu item titled ‘Login’. Works fine but may not be the best way, so critique is welcome.

function wpse0520_modify_login_menu_item( $item ) {

    if(isset($item->title)){
        if( $item->title =='Login' ) {
            if(is_user_logged_in()){
                $item->url = wp_logout_url( get_permalink() );
                $item->title="Logout";
            } else {
                $item->url = site_url( 'login' );
            }
        } elseif ( $item->title =='Logout' ) {
            if(!is_user_logged_in()){
                $item->url = site_url( 'login' );
                $item->title="Login";
            } else {
                $item->url = wp_logout_url( get_permalink() );
            }
        }
    }
    return $item;
}
add_filter( 'wp_setup_nav_menu_item', 'wpse0520_modify_login_menu_item' );

Related Posts:

  1. How reduce wordpress login session timeout time?
  2. How long do users stay logged in if they DON’T check remember me?
  3. how to logout user on browser tab or window closes
  4. Custom Connect to Facebook, problem logging in/logging out
  5. Session Experies and Get Logged Out Within Few Minutes
  6. WordPress error on log out ‘Not Permitted’ and can’t log out
  7. Add logout link when logged in, make it disappear when logged out?
  8. Lock out all WordPress Administrators except two specific users
  9. How to remove ‘wordpress…’ text from page titles in tabs
  10. Problem with is_user_logged_in() function in some pages
  11. Why deleting/removing cookies in WordPress does not log me out from admin?
  12. wp_logout logging everyone out instead of just the user that clicked the logout link!
  13. Inconsistent login state
  14. Can i hide a dynamically created div to logged out users?
  15. In Django, how do I know the currently logged-in user?
  16. Can I programmatically login a user without a password?
  17. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  18. Is there any way to rename or hide wp-login.php?
  19. How to login with email only no username?
  20. How can I redirect user after entering wrong password?
  21. Increase of failed login attempts, brute force attacks? [closed]
  22. Login page ERROR: Cookies are blocked due to unexpected output
  23. Separate registration and login for different roles
  24. SSO / authentication integration with external ‘directory service’
  25. Preventing session timeout
  26. How to prefill WordPress registration with social details
  27. Check for correct username on custom login form
  28. Disallow user from editing their own profile information
  29. I can’t access my site via wp-admin
  30. ‘Password field is empty’ error when using autofill in Chrome
  31. Removing username from the ‘wordpress_logged_in’ cookie
  32. How to show ‘login error’ and ‘lost password’ on my template page?
  33. What is $interim_login?
  34. Custom login form
  35. How to prefill the username/password fields on the login page
  36. wp_signon returns user, but the user is not logged in
  37. Adding extra authentication field in login page
  38. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  39. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  40. wp_signon() does not authenticate user guidance needed
  41. What exactly is ReAuth?
  42. What are the differences between wp_users and wp_usermeta tables?
  43. Login members using web services
  44. Make my wordpress blog remember my login “forever”
  45. How to check in timber if user is loggedin?
  46. How do I change the language of only the login page?
  47. Disable WordPress 3.6 idle logout / login modal window / session expiration
  48. How to change the default logout link on WordPress Admin
  49. Stop WordPress from logging me out (need to keep me logged in)
  50. Woocommerce registration page [closed]
  51. How to disable autocomplete on the wp-login.php page
  52. Share login data/cookies between multiple installations
  53. Synchronize WordPress user accounts across multiple domains and installations without using WordPress MU
  54. How to pass users back and forth using session data?
  55. How do I change the logo on the login page?
  56. Why does WordPress hide the reset password key from the URL?
  57. Is it possible to sign in with user_email in WordPress?
  58. How to use current_user_can()?
  59. Avoid to load default WP styles in login screen
  60. WordPress registration message
  61. How to fake a WordPress login?
  62. how to display the wordpress login and register forms on a page?
  63. Does wp_logout_url() destroy a session? (Logging out question)
  64. How can I send a welcome email to a user AFTER they login for the first time?
  65. Can not login with correct username and password
  66. Website Visible only to Registered users
  67. How can i increase the login expiration length?
  68. How do I use add_action from a class method?
  69. How to remove the WordPress logo from login and register page?
  70. How can I add a custom script to footer of login page?
  71. Brute force attack?
  72. Customize wp_new_user_notification_email()
  73. Need to execute a cron job
  74. Masking logout URL
  75. Login email after registration never sent or received
  76. How can I create a separate blog that is private?
  77. How to keep always logged in development environment
  78. Add Confirm Password field in wp-login.php Password Reset page
  79. handling login/logout redirects
  80. Integrate recaptcha and wp_signon – what is needed?
  81. Stop users from logging in from multiple locations
  82. I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
  83. custom login page redirect to logged in user profile page
  84. Email address or username used to login in wordpress
  85. How do I check if a post is private?
  86. Front-end login: Redirect user to the post they had created
  87. Receiving “This content cannot be displayed in a frame” error on login page
  88. My login form does not work
  89. share login/logout sessions across two installs?
  90. Change “You are now logged out” text
  91. Programmatically log in a wordpress user
  92. Action wp_login_failed not working if only one field is filled out
  93. Getting “Cookies are blocked or not supported by your browser” on login page
  94. What is the purpose of logging out after WordPress upgrade?
  95. Is it alright for two people to simultaneously be logged into a WP site as administrator?
  96. wp-login.php redirecting to HTTPS
  97. Display last login time
  98. How to customise wp-login.php only for users who are setting a password for the first time?
  99. Intentionally Force Failed Login WordPress
  100. How do I turn off the ability to login?
Categories login Tags login, logout
Allow author to choose his featured image size
How to Display Custom Meta Box only on Specific Page IDs

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress