Ask Your Question
1

In my React application with a backend of Wordpress, how can I specify the SameSite attribute to determine whether a cookie should be sent in a cross-site request, as currently there is an error?

asked 2021-09-03 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-03-05 13:00:00 +0000

ladyg gravatar image

To set the SameSite attribute for cookies in a React application with a backend of WordPress, you need to modify the server configuration to add the attribute to the cookie header. WordPress, by default, sets the SameSite attribute to Lax, but if you need to set it to strict, you can modify the code.

Add the following code to the WordPress theme's functions.php file:

add_filter('wp_headers', 'set_cookie_samesite');
function set_cookie_samesite($headers) {
    if ( isset($headers['Set-Cookie']) ) {
        $headers['Set-Cookie'] = str_replace('secure;', 'secure; SameSite=None; Secure;', $headers['Set-Cookie']);
    }
    return $headers;
}

This will modify the WordPress Set-Cookie header to include the SameSite=None; Secure; attributes, which will allow the cookie to be sent in a cross-site request.

Note: Make sure your back-end is configured to use a secure connection (HTTPS) as the Secure option is employed.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-03 11:00:00 +0000

Seen: 7 times

Last updated: Mar 05 '23