Reputation: 91
In my store there is an iframe in which products are placed. Iframe has the ability to save the selected product configuration, but only authorized users can do this.Thus, need to check whether the token has entered under login or not. Wordpress provides the function is_user_logged_in () but it does not suit me.My question is this: is there a way to do the same through the REST API?
Upvotes: 0
Views: 567
Reputation: 14467
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
The WP REST ajax call itself by default doesn't carry over the cookie. You can follow the above ref, using this way:
In PHP:
<?php
wp_localize_script( 'wp-api', 'wpApiSettings', array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
) );
In JS:
$.ajax( {
url: wpApiSettings.root + 'wp/v2/posts/1',
method: 'POST',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data:{
'title' : 'Hello Moon'
}
} ).done( function ( response ) {
console.log( response );
} );
WP will automatically setup the user in REST with that. And you can use is_user_logged_in()
now.
Upvotes: 1