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

Set logged in user based on API response

init is probably a good hook to do the API call, if you need know somehting about the current WP user as the user is authenticated by that time. You can check the WP user login status with is_user_logged_in(), if needed.

Or if you need to do some redirecting, then template_redirect could also be a viable hook. If you just want to change the page template, then determine the correct template inside template_include filter.

WordPress has its own HTTP API that you could perhaps use to make the API call by using wp_remote_get(). But I guess cUrl will work as well, if you prefer to use it.

If you also need to create a new user to WP and not just to your API, you can use wp_insert_user() and then use wp_signon() to log the new user in.

Here’s a rough example regarding wp_remote_get(),

function check_login_status() {

  // if relevant to check wp login status
  // if ( is_user_logged_in() ) {
  //   // do something
  // }

  $api_url="http://api.isuserloggedin.com";
  // defaults shown
  $api_request_args = array(
    // 'timeout'     => 5,
    // 'redirection' => 5,
    // 'httpversion' => '1.0',
    // 'user-agent'  => 'WordPress/' . $wp_version . '; ' . home_url(),
    // 'blocking'    => true,
    // 'headers'     => array(),
    // 'cookies'     => array(),
    // 'body'        => null,
    // 'compress'    => false,
    // 'decompress'  => true,
    // 'sslverify'   => true,
    // 'stream'      => false,
    // 'filename'    => null
  );
  $api_request = wp_remote_get( $api_url, $api_request_args );

  if ( ! is_wp_error( $api_request ) ) {
    $response = wp_remote_retrieve_body( $api_request );
    // if needed, decode the $response
    // $response = json_decode( $response );

    // do something with the $response

  } else {
    // api call failed    
  }

}
add_action( 'init', 'check_login_status' );

Related Posts:

  1. What would be the best way to implement Magic Link logins in WordPress?
  2. Best option to implement external register/login to WP from self-made API
  3. Login and register by API
  4. How to use WordPress REST API to login a user with dart/flutter?
  5. SSO / authentication integration with external ‘directory service’
  6. Give visitor access to password protected page/post via external script
  7. Custom Connect to Facebook, problem logging in/logging out
  8. Remote REST request to check login status
  9. WordPress Security – How to block alternative WordPress access
  10. How to Get Logged-in to “Remote WP Site” from my local script (in Same Browser)?
  11. How to set JWT token with PHP on successful login?
  12. SSO to WP, from a non-wp site on a different domain and server
  13. Login with serialized password
  14. Is the login encrypted before it is sent? If so how to do I encrypt it the same way?
  15. How to authenticate a user with an external webservice
  16. Get WordPress login functions without printing anything
  17. Is there a way to call via javascript if a user is logged-in on a static html file?
  18. How the wordpress login and signup in react native app
  19. How to use google api for wordpress login
  20. Log in to wordpress after executing another form function
  21. Login user after registration programmatically
  22. In Django, how do I know the currently logged-in user?
  23. Can I programmatically login a user without a password?
  24. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  25. Is there any way to rename or hide wp-login.php?
  26. How to login with email only no username?
  27. How can I redirect user after entering wrong password?
  28. WordPress Rest API custom endpoint optional param
  29. Increase of failed login attempts, brute force attacks? [closed]
  30. Using the Rewrite API to Construct a RESTful URL
  31. Login page ERROR: Cookies are blocked due to unexpected output
  32. Separate registration and login for different roles
  33. Preventing session timeout
  34. How reduce wordpress login session timeout time?
  35. How to prefill WordPress registration with social details
  36. Check for correct username on custom login form
  37. Disallow user from editing their own profile information
  38. I can’t access my site via wp-admin
  39. ‘Password field is empty’ error when using autofill in Chrome
  40. Removing username from the ‘wordpress_logged_in’ cookie
  41. How to show ‘login error’ and ‘lost password’ on my template page?
  42. Query WP REST API v2 by multiple meta keys
  43. What is $interim_login?
  44. Custom login form
  45. How to prefill the username/password fields on the login page
  46. wp_signon returns user, but the user is not logged in
  47. How do I cache (core) API requests?
  48. Adding extra authentication field in login page
  49. WP REST API only returning partial list of users
  50. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  51. Fetch All Posts (Including Those Using a Custom Post Type) With WordPress API
  52. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  53. wp_signon() does not authenticate user guidance needed
  54. What exactly is ReAuth?
  55. What are the differences between wp_users and wp_usermeta tables?
  56. WP REST API no longer supports filter param, so how do I get posts in a custom taxonomy?
  57. Is there a way to get protected meta fields through any of the available built-in WordPress APIs? (xmlrpc, wp-json)
  58. Login members using web services
  59. Make my wordpress blog remember my login “forever”
  60. How to check in timber if user is loggedin?
  61. How do I change the language of only the login page?
  62. Disable WordPress 3.6 idle logout / login modal window / session expiration
  63. Stop WordPress from logging me out (need to keep me logged in)
  64. Woocommerce registration page [closed]
  65. How to disable autocomplete on the wp-login.php page
  66. Share login data/cookies between multiple installations
  67. Synchronize WordPress user accounts across multiple domains and installations without using WordPress MU
  68. How to pass users back and forth using session data?
  69. Slow REST API calls on self-hosted WordPress [closed]
  70. How do I change the logo on the login page?
  71. Why does WordPress hide the reset password key from the URL?
  72. Is it possible to sign in with user_email in WordPress?
  73. Is it safe to fix Access-Control-Allow-Origin (CORS origin) errors with a php header directive?
  74. How to use current_user_can()?
  75. Avoid to load default WP styles in login screen
  76. WordPress registration message
  77. How to fake a WordPress login?
  78. how to display the wordpress login and register forms on a page?
  79. Does wp_logout_url() destroy a session? (Logging out question)
  80. Retrieving pages with multiple tags using REST API
  81. How can I send a welcome email to a user AFTER they login for the first time?
  82. Can not login with correct username and password
  83. How to allow users login to WP from external domain and make REST requests
  84. Website Visible only to Registered users
  85. How can i increase the login expiration length?
  86. How do I use add_action from a class method?
  87. how to authenticate for the REST API from a plugin and from command line
  88. How to remove the WordPress logo from login and register page?
  89. How can I add a custom script to footer of login page?
  90. Brute force attack?
  91. Why is per_page not working with categories in WP API?
  92. Customize wp_new_user_notification_email()
  93. How to check WordPress website username and password is correct
  94. Need to execute a cron job
  95. Login email after registration never sent or received
  96. How can I create a separate blog that is private?
  97. How to keep always logged in development environment
  98. Add Confirm Password field in wp-login.php Password Reset page
  99. WordPress API – Get Drafts
  100. REST API: How can I restrict a custom post type to only be accessible by authenticated users?
Categories login Tags api, login, remote-login, rest-api
Warning: sprintf(): Too few arguments in nav-menu-template.php
How to create page with post content in it?

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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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