If you want to hide part of your WordPress post content and only reveal it after a user comments or replies, this tutorial is for you. This feature is easy to implement and can boost user engagement.
Core Implementation Code
Add the following shortcode function to your theme's functions.php file.
// WordPress shortcode to hide content until comment/reply
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<p class="reply-to-read">Note: This content is hidden. Please <a href="#respond" rel="external nofollow" title="Comment on this post">comment</a> to view it.</p>'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
// Allow site admin to always see content
$admin_email = "[email protected]"; // CHANGE THIS
if ($email == $admin_email) {
return $content;
}
} else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {
$email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);
} else {
return $notice;
}
if (empty($email)) {
return $notice;
}
global $wpdb;
$post_id = get_the_ID();
$query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1";
if ($wpdb->get_results($query)) {
return do_shortcode($content);
} else {
return $notice;
}
}
add_shortcode('reply', 'reply_to_read');
How to Use
1. Place the code in your theme's functions.php.
2. In the post editor, wrap the content you want to hide with the [reply] shortcode:
[reply]This is hidden content visible only after commenting.[/reply]
Visitors will see a notice prompting them to comment. Once they submit an approved comment, the hidden content will appear.
Important Notes
- Admin Email: Change
[email protected]to your actual email so you can always see hidden content when logged in. - Shortcode Name: The shortcode is registered as
[reply]. You can change the first parameter inadd_shortcode('reply', 'reply_to_read');to use a different tag. - How It Works: The code checks if the visitor's email matches an approved comment on the post. It works for logged‑in users and guests (if cookies are present).
- Caching: If you use a caching plugin, you may need to exclude pages using this shortcode from full‑page cache, as it relies on dynamic user data.