Skip to content

Snippets / PHP

PHP JS HTML

Ability to Show Users "Post Read Status"

D Donald McGuinn · 241 views · Updated 2 years ago
These snippets will add the functionality for people to "Record" if they have seen a post or not. Works for both logged in and non-logged in users (non logged in through cookies so it will eventually expire). Change the text to whatever you want.
function mark_post_as_read() {
if (is_single()) {
global $post;
if (is_user_logged_in()) {
$user_id = get_current_user_id();
$read_posts = get_user_meta($user_id, 'read_posts', true);
$read_posts = empty($read_posts) ? array() : (array) $read_posts;

if (!in_array($post->ID, $read_posts)) {
$read_posts[] = $post->ID;
update_user_meta($user_id, 'read_posts', $read_posts);
}
} else {
// Non-logged-in users: use JavaScript (below) to handle local storage
echo 'markPostAsRead(' . $post->ID . ');';
}
}
}
add_action('wp', 'mark_post_as_read');

 

function show_read_status() {
global $post;
$output = '

You have not read this post.

';

 

if (is_user_logged_in()) {
$user_id = get_current_user_id();
$read_posts = get_user_meta($user_id, 'read_posts', true);

if (is_array($read_posts) && in_array($post->ID, $read_posts)) {
$output = '

You have read this post.

';
}
} else {
// Non-logged-in users: JavaScript will handle showing the status
$output .= 'showReadStatus(' . $post->ID . ');';
}
return $output;
}
add_shortcode('read_status', 'show_read_status');
PHP · 50 lines
function markPostAsRead(postId) {
let readPosts = JSON.parse(localStorage.getItem('readPosts') || '[]');
if (!readPosts.includes(postId)) {
readPosts.push(postId);
localStorage.setItem('readPosts', JSON.stringify(readPosts));
}
}

function showReadStatus(postId) {
let readPosts = JSON.parse(localStorage.getItem('readPosts') || '[]');
if (readPosts.includes(postId)) {
document.write('You have read this post.

');
} else {
document.write('You have not read this post.

');
}
}
JS · 20 lines
Shortcode to add to the preview card.
[read_status]
HTML · 1 lines

Discussion 3

Ask a question or share how you used this.

Log in to join the discussion.
artinkupar 1 year ago

Hi, thanks for this code, but could you please tell me where to add the JS code snippet?

Donald McGuinn 1 year ago

There is a blue box above the snippet that refers you to where to add it ;)

artplaysrl 1 year ago

Is it possible to display this on preview cards in listings (catalogues)?

Donald McGuinn 1 year ago

Yes.

Rocky 2 years ago

Is it possible to show a graphic element (like a border or an icon) and not a text if the post has been visited or not? I would like to add this snippet on my preview card and show a grey border if the post has been visited and red if the post is not yet visited. Thanks

Donald McGuinn 2 years ago

You would have to completely rewrite the code to do so.

Voxel Guide assistant
Ask about snippets, addons & how-tos

Hey — how can I help?

I can dig through the library for snippets, addons, and tutorials.

AI can be wrong — double-check anything important.