I'm trying to use setcookie
inside a WP REST request, but it isn't working. The REST request runs properly and performs it's other duties well, but it doesn't set any cookies.
Here's my example code, which is running on mywebsite.com
add_action( 'rest_api_init', function () {
register_rest_route( 'my_auth/v1', '/auth_login', array(
'methods' => array('POST'),
'callback' => 'auth_login',
));
});
function auth_login( WP_REST_Request $request ) {
update_post_meta(1234, 'test_field', 'test_value'); // this works!
setcookie('auth_token', 'test1234', time()+3600, "/", 'mywebsite.com'); // this doesn't work
return 'test';
}
If I send an AJAX request to my endpoint (mywebsite.com/wp-json/my_auth/v1/auth_login
), the update_post_meta
call works fine, but the setcookie
call does not. I have tested this by visiting mywebsite.com
after a request, which has no cookies set.
set-cookie
directive in the return headers from the rest call?Set-Cookie:auth_token=xxxx-xxxx-xxxx-xxxx-xxxx; expires=Fri, 06-Jan-2017 18:08:37 GMT; Max-Age=3600; path=/; domain=mywebsite.com
- but they don't get set.Access-Control-Allow-Credentials: true
in my headers may work - giving that a try now.