How to get cookie value?

Because my reply is too long to be a comment, so I write it as suggested methods instead.

Method 1

In my project, I use ajax to set cookie with php built-in setcookie() function and cookie variable $_COOKIE, I did not add them in any hook and residing in its own class, any actions calling is calling the class and/or methods from other place. So the ajax call can fetch the cookie value any time because each call is a new page loading to ajax.

For ajax, you may refer to [this post] (How to get dynamically custom post type that are under a certain category) which I have created a sample of ajax. And you may refer to Ajax handbook

Method 2

You may consider to create 1 custom meta_key for user to store the temporary value of your choice, you may set a simple array or variable.
Please note that the array need to serialize first before storing to MySQL database. The array structure is as simple as possible because it is easier to handle even it is serialized.

So you could use get_user_meta function to fetch the value

Method 3

If method 1 is working but not enough for page to page transition, you may combine method 1 and method 2 together. eg. You can fetch the value to the cookie from user meta if available.(In case any accidental events such as browser closing.) So that the user may still use a previous value without start over again.

Pros

  • can save the meta any time when you can get it
  • can make use of cookie as transition between pages
  • user meta can be a temporary storage provided for future use or other events which might be able to improve the user experience