现在很多网站,特别是资源站喜欢把一些重要内容设置为回复可见。如果主题不支持这个功能的话,只能安装第三方插件了。
本着能用代码绝不用插件的原则,分享一个纯代码实现WordPress评论回复可见的方法。
经软件迷测试,DUX主题可用!其他主题请自行测试
编辑wordpress主题文件的functions文件,在底部加入以下代码:
/**
* WordPress评论回复可见的实现方法
* https://www.dujin.org/5680.html
*/
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//对博主直接显示内容
$admin_email = "[email protected]"; //博主Email
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');
之后我们在编写文章的时候就可以添加短代码来实现wordpress回复可见的功能了!注意,将”>”改为”]”方可实现。
[reply>评论可见的内容[/reply>
[reply notice=”自定义的提示信息”>评论可见的内容[/reply>
测试