때때로 우리는 가치 있는 글을 작성한 후, 수고한 노력을 보호하거나 무단으로 가져가는 사람들에게 기회를 주지 않기 위해, 또는 댓글 수를 늘리기 위해 댓글을 남겨야 볼 수 있는 기능을 사용하고 싶을 때가 있습니다. 저는 지경조의 댓글 작성 후 공개 기능을 참고한 다음 그의 코드를 독립적으로 분리했습니다. 아래에서 여러분과 공유하겠습니다.
코드
// 评论可见
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<p class="reply-to-read">此处内容需要<a href="#respond" title="评论本文">评论</a>后才能查看.</p>'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
$admin_email = "xinyewl@qq.com";
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');
코드의 xinyewl@qq.com을 자신의 관리자 이메일로 변경하면 해당 이메일의 사용자에게 직접 콘텐츠를 표시합니다.
위 코드를 테마 디렉터리의 functions.php에 넣으면 바로 사용할 수 있습니다.
호출 방법
비주얼 편집기 또는 편집 창에 단축 코드 [reply]숨겨진 콘텐츠[/reply]를 입력하면 됩니다. 중간 부분은 숨기려는 콘텐츠로 변경할 수 있습니다.
