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

External Authentication, session_tokens not destroyed on logout

Here is the final code to get sessions to write to the usermeta and delete on logout. This code works with WordPress 4.5.2. The code is for user_id information only.

add_action( 'after_setup_theme', 'new_login' );

function new_login() {

    $user_id = ''; // Change your code to grab user_id from external source

    if ( $user_id > 0 && ! is_user_logged_in() ) {
        $user = get_user_by( 'id', $user_id );

        wp_clear_auth_cookie();
        wp_set_current_user( $user->ID, $user->user_login );
        wp_set_auth_cookie( $user->ID, true, is_ssl() );

        if ( is_user_logged_in() ) {
            return true;
        }

    } elseif ( $user_id == 0 && is_user_logged_in() ) {
        wp_logout();
        wp_set_current_user( 0 );
    }
}

There are a couple of key issues. First, the conditional ! is_user_logged_in() must be included with the check for a user_id greater than zero. Next, the wp_set_current_user(0) must follow the wp_logout() in order to avoid a refresh.

I hope this helps others trying to get external authentication working with user_id information only.

Related Posts:

  1. How should you hook a session_start() when authoring a plugin?
  2. Init action hook running late after PayPal’s return url?
  3. Two-step login process – Is it possible?
  4. Get user logged in status from within a plugin. $current_user not defined
  5. Create a Custom Login System in WordPress [closed]
  6. Difference Between Filter and Action Hooks?
  7. How many times will this code run? (or, how rich is grandma?)
  8. Which hook should be used to add an action containing a redirect?
  9. add_action hook for completely new post?
  10. Add a new tab to WordPress Plugin install Listing
  11. Changing Plugin Load Order
  12. Can someone explain what wp_session_tokens are, and what are they used for?
  13. WordPress and PHP Sessions – Security and Performance
  14. Admin settings update updating every time home page is hit?
  15. WordPress Hook for user register
  16. Global Objects and Public Methods
  17. Using filters and actions for plugin API?
  18. Is there widely accepted phpDoc syntax for documenting which hook calls a function?
  19. Explanation of the “posts_join” and “posts_fields” filter hooks?
  20. Does using set_transient() function can lead to MySQL problems?
  21. How do you use the plugin boilerplate loader class to hook actions and filters?
  22. How Do I Load My Action Earlier Enough?
  23. How to check WordPress website username and password is correct
  24. Does WP fire delete_post when trashed posts are automatically deleted?
  25. Log in from one wordpress website to another wordpress website
  26. What’s the earliest point I can get the queried object ID?
  27. How do I Make a Theme “plugin-ready”?
  28. Why does do_action pass a blank string as the first parameter if no $arg is set?
  29. WordPress scheduled task is called but not executed
  30. Problems after wp_set_password() containing an apostrophe
  31. How to add a link to WordPress Plugin install Listing
  32. How to modify post content before writing to database?
  33. I can’t find where a hook is being defined in a plugin – Easy Digital Downloads
  34. Call activation hook multiple times
  35. How do I handle multiple Submit buttons in plugin’s option page?
  36. What’s the difference between hooks, filters and actions? [duplicate]
  37. How to use filter hook ‘post_updated_messages’ in coherence with action hook ‘save_post’
  38. How to check if certain plugin exists and in expected version
  39. Plugin development, hooks, generate content
  40. Is it possible to create an action hook using do_action() within add_action()?
  41. Sessions not creating correctly in custom function
  42. How can I hook into existing WordPress Bulk actions?
  43. Hook for post permalink update
  44. Dynamically Override Fancy Title
  45. Get returned variable from a function to add_shortcode function
  46. Hook event for upload image in the menu
  47. Help with callback using do_action_ref_array
  48. How do I approach removing menu items on the fly based on settings in my plugin?
  49. Earliest WP Hook to Safely and Reliably Redirect
  50. Define a function outside a class and call the function using action or filter hook
  51. Is it possible to modify the media library admin screen?
  52. What function to hook for changes made in status and visibility of a post
  53. Create a plugin to change the action to which a function is hooked
  54. Can I differentiate between “Delete Post Permanently” and “Empty Trash” and do something for each accordingly?
  55. Synchronize Custom post type tags to WordPress default posts tags
  56. Adding Automatically To In WordPress Using Filter Referencing?
  57. Order of Operation for these three hooks
  58. comment_email hook doesn’t seem to work for comment editor field
  59. Best Practices for Creating and Handling Forms with Plugins?
  60. Is there a better way to implement responsive images than what WordPress uses by default?
  61. Modifying values with add_action to be sent to db
  62. Woocommerce – Hide a Column in Cart Table
  63. unable to stop loop when using wp_insert_post with publish_post hook
  64. save_post hook not triggered in WP v3.6.1
  65. Hook into theme-switching later than `setup_theme`
  66. Add tab to profile.php wordpress plugin development
  67. Is it possible to delay execution of shortcode output callback?
  68. WordPress Ajax callback function from plugin – OOP
  69. Hook to get image filename when it is uploaded
  70. How to display a message about updates in the plugin list
  71. Why my database table not created on activation?
  72. How do I change the initial quantity of a product in woocommerce?
  73. How to get session token of current user in wp_login hook?
  74. Is there any way to check for user login and send him to login?
  75. How to delete an user including data from custom database tables
  76. Act on user meta updated, but only once
  77. Where and how does WordPress store and use its plugin and hook information?
  78. Is there any way to allow a plugin to write over a previous version?
  79. Which are the hooks run before/after when a category’s deletion?
  80. Should action callbacks start with a verb?
  81. Execute Hook on the footer or header after activating a plugin
  82. Why is my javascript not invoked in my hooks except wp_head?
  83. edit_{$taxonomy} | Hook
  84. WordPress after content Hook & external template part
  85. Namespaced action and filter tags
  86. Proper way to pass credentials in a custom login form to avoid “headers already sent”
  87. What filter should I use to insert a button inside on Media>Add New
  88. best practice for query string values – get_query_var always empty for my value supplied in query string
  89. get_current_screen() return null
  90. Modifying meta tags after doing ajax call in plugin
  91. how to add custom functionality after woocommerce place order button
  92. .mo translation strings not loading in PHP scripts that handle AJAX calls
  93. How to use a hook to override an update_post_meta call in a plugin?
  94. Verify if user is wordpress logged in from another app since wordpress 4.0
  95. Where to add hooks in a class
  96. Trying to understand order and frequency of action hooks firing
  97. How to use output buffering in WordPress hooked functions?
  98. How to customize login process
  99. Conditional hook based on the core function that is calling it
  100. Adding option to Gallery shortcode
Categories plugin-development Tags hooks, login, plugin-development, session
Trying to remove post thumbnail with plugin
pass query string on url to filter media

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