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

Get user logged in status from within a plugin. $current_user not defined

One question: is the 3rd party that’s sending the request passing a user cookie?

If not, then WP will treat the request like any other guest! If it is, have you inspected $_COOKIE when send_response() runs?

Update: Thought I’d try and wrap up the discussion below, once and for all 🙂

You’ll have to maintain a database log of signed-in users

I thought WP was doing that already!

No, WP simply validates the cookie (if one was sent) on each request.

I thought the cookies were stored in the browser

Yup.

So if the same user is logged into my site, and then visits the sub-domain site, which calls file_get_contents(), why aren’t the cookies there?

Because the API request originates from the server, not the browser. That’s like asking why you’re not logged in with Chrome after you signed in using Firefox!

I’d like to retract my suggestion about maintaining a log of sign-ins, since I believe there are two better, easier solutions;

  1. Fire the request on the client-side (i.e. with AJAX) – since it originates from the user’s brower, the cookies are sent along as you would expect
  2. Send the user’s cookie along with your API request on the server side* (see below)

Example code for sending remote get with client’s cookies:

$data = array();

if ( ! empty( $_SERVER['HTTP_COOKIE'] ) ) {
    foreach ( explode( '; ', $_SERVER['HTTP_COOKIE'] ) as $pair )
        $data['cookies'][] = new WP_Http_Cookie( $pair );
}

// Wp_Http_Cookie will choke if cookie value has special characters.
add_filter( 'wp_http_cookie_value', 'rawurlencode' );

if ( $get = wp_remote_get( $url, $data ) ) {
    // Yippee!
}

However, 2) comes with a catch; the cookies needed to validate the API request must be sent to the script that fires it (the mention of subdomains flags a potential problem here).

If you’re using MultiSite with subdomains, this is taken care of. Otherwise, you’ll need to force the cookie domain in your wp-config.php:

 define( 'COOKIE_DOMAIN', '.example.com'  );

See that dot prefix? This indicates that the cookie should be sent to the domain or any of it’s subdomains (whereas by default, WP will set it to apply only to the main domain).

Related Posts:

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

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