0

I've collected an array and set-up a cookie via functions.php, both work fine. However, I really need to post the data to the function and set the cookie as this information will vary per customer.

The array is of product IDs collected after WP_Query.

$ids = array();

Here is my cookie code, I'd need to replace the 'ID_ARRAY' text with the actual array.

add_action( 'init', 'resultsCookie' );
function resultsCookie() {

   setcookie( 'your-results', 'ID_ARRAY', time() + 3600, COOKIEPATH, COOKIE_DOMAIN );
}
0

You could try serializing the array so that it becomes a flattened textual representation of the values it contains. e.g.

$testArr = [3,4,"test", 56];
echo serialize($testArr);

gives:

a:4:{i:0;i:3;i:1;i:4;i:2;s:4:"test";i:3;i:56;}

However, you should probably consider using user sessions for this rather than cookies.

1
  • My thinking was that cookies would be preferable as the user would most likely not be signed in, and therefore it would be good if the data could be stored beyond the initial session. Not sure how serailising data posts to the function though, could you explain? – user2994718 Jul 8 '15 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.