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

Login/logout in header

Here is a function i use to display the log in/out and registration URLs

//Add login/logout link to naviagation menu
function add_login_out_item_to_menu_wpa84418( $items, $args ){

    //change theme location with your them location name
    if( is_admin() ||  $args->theme_location != 'LOCATION' )
        return $items; 

    $redirect = ( is_home() ) ? false : get_permalink();

    if( is_user_logged_in( ) )
        $link = '<a href="' . wp_logout_url( $redirect ) . '" title="' .  __( 'Logout' ) .'">' . __( 'Logout' ) . '</a>';
    else {
        $link = '<a href="' . wp_login_url( $redirect  ) . '" title="' .  __( 'Login' ) .'">' . __( 'Login' ) . '</a>';
        if (get_option('users_can_register'))
            $link = wp_register('<li>', '</li>', false) . $link;

    }
    return $items.= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>';

}
add_filter( 'wp_nav_menu_items', 'add_login_out_item_to_menu_wpa84418', 50, 2 );

Just make sure you change theme LOCATION with your them location name

Related Posts:

  1. How to log out everywhere else, destroy all sessions “all other devices”?
  2. Change the footer text on the login page
  3. How to: PHP Log Out Link?
  4. getting logged in user info (wp-load.php) from parent directory
  5. Log in / Log Out Custom Button
  6. How to display login form anywhere, when user isn’t logged in, without redirecting?
  7. Can’t log in to WordPress wp-admin after adding code to functions.php
  8. Check if user had autologin & if so, logout
  9. Log out without confirmation request (nonce)
  10. Having trouble creating two shortcodes, one for logged in user and one for visitors
  11. Restrict wordpress access to logged users only
  12. Menu not updating for logged in users after redirect
  13. how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
  14. Change Login or Logout text based on status
  15. Refresh page after login with litespeed cache
  16. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  17. is_user_logged_in() not working in homepage
  18. Unable to logout correctly after wp-login file was modified
  19. ACF: how do I get the fields and its values of a specific group?
  20. How to store data from multiple forms using ajax and php
  21. How to control WordPress image metadata (using Imagick)?
  22. How to add seperate classes to no-search-result and found-search-result pages on wordrpess search – is_search()
  23. Get value from shortcode to do something
  24. PHP 8, AJAX mail form to function.php doesn’t work
  25. How do I add custom bulk actions to multiple custom post types?
  26. Error when adding excerpt to the content through functions.php
  27. Automatically adding new post categories to menu
  28. Add data attribute to each li in menu
  29. Get post id in a function when edit/add a post
  30. Making BuddyPress Activity Stream (Post Type) Featured Images Clickable [closed]
  31. How to check if a meta value has already been assigned to any user?
  32. How to complete two other input fields, completed the first
  33. Prev/Next child navigation for current page modifications?
  34. Adding rel tag to all external links
  35. How can I apply_filters from inside a function?
  36. function to assign user role based on a field from usermeta
  37. what is the best practice to add new field to an api route
  38. Retrieve $_POST data submitted from external URL in WordPress(NOT API)
  39. Programmatic Login from 3rd Party site
  40. Pass Category Name, Description and Photo into variables to pass to jQuery
  41. Use DOMDocument with ob_start breaks my HTML code
  42. Auto-update products after they were published – Woocommerce
  43. How to prevent WP_Query function from returning all posts when empty?
  44. wp_trim_words() does not work with my code Am I doing any mistake in my code?
  45. JS file work only in index page
  46. Display a specific category of products in shop page and disable code for specific actions
  47. Woocommerce related product text
  48. esc_html_e() is not translating string in wordpress [closed]
  49. Removing “wpautop” (auto tags) only on certain pages?
  50. Fatal error: Uncaught Error: Call to undefined function test()
  51. How to pick the default selected value in wordpress dropdown?
  52. Cannot access wp-admin/wp-login.php (WordPress backend) anymore, what could be wrong?
  53. Using WP-API and SSE not authenticating user ID
  54. Woocommerce: hook action/filter I could use to add variation id and price with each attribute opt on WooCommerce Rest api
  55. Validate functions before inserting then into functions.php
  56. How to retrieve current wordpress profile page URL?
  57. Firing schema via code in functions.php doesn’t work
  58. A non-numeric value encountered in /wp-includes/functions.php on line 68
  59. Get page that displays all children of taxonomy parent
  60. Ajax request not sending to server and returning – wp-admin/admin-ajax.php 400
  61. get author_name from queried post
  62. How to debug my custom login form looping intermittently
  63. Why ajax doesn’t work on certain wordpress hooks?
  64. Is it possible to intercept all ajax requests and get the parameters and the returns?
  65. How to call plugin function per site in a multisite?
  66. Print last modified date only on posts
  67. different id for same element visual composer [closed]
  68. WooCommerce: How can I get orders with a custom ID in order meta data object?
  69. Tax query in pre_get_posts not working
  70. WordPress Gravatar filter is removing my custom attributes
  71. foreach loop still echoes array
  72. trigger html cleanup for all posts
  73. Error in custom php function doesn’t exist
  74. dynamic dependent select dropdown
  75. Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
  76. Custom shortcode outputs plain text instead of HTML at top of post
  77. how can i use $_COOKIE[] + PostID to execute the below code?
  78. Return a numerical function value in Customizer controls
  79. How can I get the values of my WordPress $wpdb query in Jquery?
  80. What’s the uses of wp_cache_set() or wp_cache_add()?
  81. Get users that likes the post
  82. Blog posts repeat
  83. Ajax load more button for comments wordpress
  84. Function to capture a value and store it in a variable
  85. How to abort saving data in save_extra_profile_fields function WordPress?
  86. Pass php dynamic variable to shortcode
  87. Unique icons next to each WordPress menu item
  88. 3 Slashes appear after Apostrophe in custom fields after updating product-site
  89. How to create a php variable using WordPress the_title() function
  90. Keyword checking in Gravity Forms
  91. how to refresh div on template part?
  92. Child page menu in sidebar
  93. Populate editor with some content of a page with a page template
  94. Title Case WordPress Menu Items
  95. I want to load in a new class but only if the current page is single-movies.php
  96. Modify function to print tags/categories/exclude tags/categories/ number of posts
  97. Incorrect amount of posts returned when filtering related Woo products by custom taxonomy
  98. Search not showing all results
  99. Validation algorithm in checkout field
  100. Css loads very slow [closed]
Categories PHP Tags functions, login, logout, php
Make user as Default contributor for all Blog Posts
Facebook doesn’t display the related thumbail for some posts

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