How to save history in the client's browser without login? [closed]
-
16-04-2021 - |
質問
I know that we can use some snippets or plugins to save the client history but this work for the login clients as I know.
My question is how to do the same thing but for unlogin users. Let's say I am a user without an account and I have viewed some posts in the website. So the next time I want to visit this site I want to see my latest posts that I have seen from the last visit in a page called history.
It's like YouTube history page but without login so that everything is saved in the user's browser like a cookie file or something else.
To use cookies is easy if I followed the php documentation but I want to use it in wordpress so that why I am asking my question here.
解決
You can use cookie to store information in the visitor's browser.
In your single post template, you can set the cookie by using
setcookie( 'last_post_id', get_the_ID() );
And on your other page, you can manage the last post that the user has visited.
$last_post_id = ! empty( $_COOKIE['last_post_id'] ) ? $_COOKIE['last_post_id'] : false;
if( $last_post_id ) {
// do something with the cookie.
}