0

enter image description here

So i need to create a cookie with the post IDs that the user has accessed(i have a WP site with 144 movie posts), and after to display these IDs on another page. I writed this code, but the problem is that when i try to print_r($_COOKIE); in the history.php page i just get the ID of the current page=)) and not the movie posts IDs that the user has viewed in the past. So i want in this history.php page to display the history search of a user(the movies that he viewed on my site).

My script:

jQuery(document).ready(function($) {
 let post_id =  my_script_vars.postID;
    document.cookie = 'Movie='+post_id+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
}); 
yaws76
  • 67
  • 7
  • 1
    You're currently just overwriting the value of the cookie instead of appending values to it so any existing value will just be replaced. Personally, I would probably use local storage for this (in my opinion, it's cleaner and easier). Then you can [store an array](https://stackoverflow.com/questions/3357553/how-do-i-store-an-array-in-localstorage) with the id's that you can add to. First get the current array, append the id and then store it again. You can do the same thing using cookies, if you prefer. – M. Eriksson May 18 '20 at 16:43

1 Answers1

2

Include post id in cookie name not to override previous one: document.cookie = 'Movie'+post_id+'=; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'

Narek Malkhasyan
  • 1,332
  • 1
  • 12
  • 22
  • when i echo the cookies in the page i get this too: "wordpress_logged_in_3a7a1158c840db9eb88059a6c751f04b|1590062105|cK5f90cqlJQuJuFoPHSg9dPACNVwfTiQZ1bosiI4K6X|9f241253114495dbffacfbfc1edb1d9eadca20f9ef1d8aae6a83836894526475 " string(33) "wp-settings-time-11588853077 string(39) "wp-settings-1libraryContent=browse................. How can i delete this from the page – yaws76 May 18 '20 at 17:22