Search code examples
wordpressreactjswordpress-rest-api

How to use WP hooks into ReactJS


In my WordPress project i need to develop an application that has a module about user profile. In WordPress there is a hook is_user_logged_in() to check if user is logged in or not. But now what is a flow to use this functionality into reactJS. As my profile data is rendering from header.jsx file.

if(user logged in){
 "then user profile will display"}
else{
 "user login screen will display"
}

Solution

  • You can do this by following the next steps:

    1. Backend. Create API endpoint to check if the user logged. For example: /wp/api/user. In this action add php code that will checking by is_user_logged_in() and send response true or false.
    2. Frontend. Create XMLHttpRequest by React ( for example I'm using axios library for this ) to URL that you created in the previous step. Finally, after getting a response you will know in your React app is a user logged.