AnswerBun.com

How to generate the COOKIEHASH from JavaScript

WordPress Development Asked by Zeth on October 26, 2020


The overarching question

How do I get the contents of the PHP-variable: COOKIEHASH inside a Cypress-test.


I’m writing some Cypress-test for WordPress, and in order to set the cookies to log in a user in WordPress using Cypress, then I need the contents of the COOKIEHASH-variable.

For those not familiar with the COOKIEHASH, go to a WordPress-installation and put in this code anywhere:

echo '<pre>';
print_r(COOKIEHASH);
echo '</pre>';
die();

And then you’ll see it. It looks something like this: a8b94154380982c3284a467b8aa224c6.

When I run my Cypress-tests for the first time, then I don’t know what that hash is, so I don’t know which cookies to set.

So currently I log in manually, go to cookies in my browser and get the hash. Which is quite the manual operation for automated tests. 🙂


My attempts / thoughts

  1. It looks like an MD5-hash. But I can’t recreate the cookie-hash with an JS MD5-function. It doesn’t correspond. I haven’t tried that many MD5-JS-functions, it should be said.

If I hash zeth.dk, then the js-md5-hash gives me: 8aa4b9bbc2a9e946b670fd8372a3081a
and I can see in my cookies that the hash is: b7d8d5a26884619e3b3b4481ba778642

  1. I considered making an API-endpoint (in my functions.php), that returns the cookie-hash. But this just feels wrong, making my code to backflips like this.

One Answer

By default it's the MD5 hash of the site URL:

    $siteurl = get_site_option( 'siteurl' );
    if ( $siteurl ) {
        define( 'COOKIEHASH', md5( $siteurl ) );
    }

This includes the scheme but not a trailing slash, so in your example you'd need the hash of https://zeth.dk.

You can also override this by defining COOKIEHASH in wp-config.php with some other value.

Correct answer by Rup on October 26, 2020

Add your own answers!

Related Questions

How to handle image resize in media_handle_sideload?

1  Asked on January 10, 2021 by user1692333

 

apiFetch security

1  Asked on January 9, 2021

 

Use cron to create a non blocking task

2  Asked on January 7, 2021 by cyclonecode

   

Creating a slider shortcode to use with bxslider

0  Asked on January 6, 2021 by devsem

   

Redirect Admin User in Dashboard

4  Asked on January 5, 2021 by plumwd

   

Adding featured image to a new post using front-end form?

1  Asked on January 3, 2021 by davood-kazemi

   

Hook when post is set from published to draft?

1  Asked on January 2, 2021 by anton-emery

       

How to execute filter with WP Cli?

0  Asked on January 2, 2021 by efbi

   

Get HTTP response code on non-2xx apiFetch request

0  Asked on January 1, 2021 by nicholas-harris

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir