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. SSO / authentication integration with external ‘directory service’
  5. Give visitor access to password protected page/post via external script
  6. Custom Connect to Facebook, problem logging in/logging out
  7. Remote REST request to check login status
  8. WordPress Security – How to block alternative WordPress access
  9. How to Get Logged-in to “Remote WP Site” from my local script (in Same Browser)?
  10. How to set JWT token with PHP on successful login?
  11. SSO to WP, from a non-wp site on a different domain and server
  12. Login with serialized password
  13. Is the login encrypted before it is sent? If so how to do I encrypt it the same way?
  14. How to authenticate a user with an external webservice
  15. Get WordPress login functions without printing anything
  16. Is there a way to call via javascript if a user is logged-in on a static html file?
  17. How the wordpress login and signup in react native app
  18. How to use google api for wordpress login
  19. Log in to wordpress after executing another form function
  20. Login user after registration programmatically
  21. Custom login form
  22. Adding extra authentication field in login page
  23. Make my wordpress blog remember my login “forever”
  24. How to check in timber if user is loggedin?
  25. Does wp_logout_url() destroy a session? (Logging out question)
  26. Customize wp_new_user_notification_email()
  27. Need to execute a cron job
  28. Login email after registration never sent or received
  29. How to keep always logged in development environment
  30. Add Confirm Password field in wp-login.php Password Reset page
  31. Gaining Login Access via the Database
  32. send users logging in from wp-login.php directly to home page of site, rather than dashboard
  33. Changing Login Logo
  34. Get user ID after logging in
  35. User logon by using mobile number [closed]
  36. Pre checking condition before login
  37. wp-admin seems to be redirecting
  38. Options to get my custom post type metadata via the WordPress API
  39. WordPress login urls
  40. Redirect after empty login username and password
  41. Change Favicon on Login Screen?
  42. Customizing the WordPress login form
  43. How to display username and password after registration
  44. Login error redirecting to wp-login page
  45. Custom login message for single post pages only
  46. Private page protected with username and password
  47. Password protect media attachment – share across guests
  48. Password reset – Disabled for LDAP accounts
  49. Autologin only working the second time
  50. Why wp_update_user doesn’t update user_activation_key on users with apostrophes in their email?
  51. Forgot Password/ Password Reset Page does not exist
  52. Login WP – Connect single field to an external api
  53. WordPress on Apache behind nginx using letsencypt issue with loginizer/limit login attempts
  54. Auto login between word press subdomain and a .net website
  55. Custom Login form from WordPress site to non-WordPress site
  56. Single sign on with custom site
  57. By registering always make uppercase the first letter of the login
  58. Enqueue new login style sheet
  59. WordPress Conditional Login Links
  60. Getting a Page via its post-name using WP REST API v2 and Postman
  61. Secure login on wordpress [closed]
  62. Do more action after login successfully
  63. How can I password protect a WordPress site without requiring users to log in?
  64. Can’t login to my own website
  65. Custom API plugin to execute 3rd party API to retrieve data
  66. REST API and filtering by meta value
  67. Cannot log into migrated site
  68. WordPress SSL not working [closed]
  69. I want login using email not username wordpress front end
  70. How to Remove default “Login” link
  71. How to unpublish my new site while working on it?
  72. How to force “remember me” users to login again?
  73. Custom Endpoint – Does it possible to use PUT method with WP API Rest?
  74. User not able to sign in after wp_authenticate() and wp_signon() wordpress
  75. Validate user login in php
  76. WordPress site login Redirect
  77. Thank You Page Layout Differs on User Logged In Status
  78. Share login credential with QR code
  79. On Homepage “is_user_logged_in()” function is not working after login and it is working after refresh the page?
  80. Cant reset password my wordpress password not even with phpMyAdmin
  81. The REST API encountered an error in wordpress?
  82. Get WordPress logged in username from root domain when WP is installed in a subfolder
  83. How to invalidate `password reset key` after being used
  84. wp-cron event doesn’t run when custom login API is enabled
  85. Does it make sense to check a nonce on user log in?
  86. unable to Login to Admin
  87. WordPress login loop on proxy setup with SSL – Apache proxy to IIS
  88. ERROR: Cookies are blocked due to unexpected output – ultimate solution
  89. WordPress REST API Endpoint (Authors and Categories latest posts)
  90. Android app – can’t login: “This site already exists in the app, you can’t add it”
  91. Fatal error: Call to undefined function get_plugin_data()
  92. backend access per user status
  93. REST API And Error Codes – No Message
  94. WordPress Login / SSL = Code Question
  95. Enqueue script on logon failure
  96. I get 404 when I try to login ever since I changed the permalink
  97. Logging in takes a few refreshes to show you are logged in, is this a cache issue? [closed]
  98. WordPress API standard compliance and specification for external database [closed]
  99. Can i hide a dynamically created div to logged out users?
  100. WordPress – Contact Form 7 – Get uploaded file issue
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
  • 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