【WordPress】内容回复可见的实现方法

优秀源码 无标签
0 906
WIZ
WIZ 自成一派 2024-02-19 20:54:52
Lv:41级

纯代码实现

在当前网站主题下的 functions.php 文件内加入以下代码:

//部分内容隐藏回复可见

function reply_to_read($atts, $content = null, $admin_email = []) {

extract(shortcode_atts(array("notice" => '<p class="reply-to-read">此处内容需要 <a href="#comments" 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 = "xxx@aaa.com"; //自己的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');

 

 

 

将代码里的博主Email邮箱替换成自己的,添加好后,保存即可!

以后在发布文章的时候,只需添加以下短代码即可实现给指定内容设置回复可见了。

[reply]

评论可见的内容

[/reply]

[reply notice="自定义的提示信息"]

评论可见的内容

[/reply]

 

 

 

自定义插件实现

上述纯代码实现方法会带来一个问题,那就是当主题更新之后,修改的内容可能会被覆盖。
同样的代码,自定义成插件可解决这个问题。

在 WordPress 安装目录下 /wp-content/plugin 新建文件夹,取名 reply-for-view,在该文件夹中新建文件 index.php 并添加以下代码。

<?php

/*

Plugin Name: Reply for View

Description: 将部分内容隐藏,回复可见。

*/

//部分内容隐藏回复可见

function reply_to_read($atts, $content = null, $admin_email = []) {

extract(shortcode_atts(array("notice" => '<p class="reply-to-read">此处内容需要 <a href="#comments" 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 = "xxx@aaa.com"; //自己的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 for View 的插件,启用即可。
使用方法跟纯代码实现方法一样。这样功能不会受主题更新影响,若插件丢失或禁用,文章内隐藏内容会直接显示出来。

回复可见

【END】

楼主签名:DNSWIZ 站长故事
回帖
回复列表

    请遵守各国法律法规 严禁违规内容

    • QQ群:1140251126
    • Email:m@max.ooo
    • 本站可以自由发布外链
    • 本站域名皆为闲置域名,均可出售
    Hot posts
    01 实现网:评价较好的程序员招聘与接单平台 162
    02 kangle更新了v3.6.0 谁有相关资料? 139
    03 php ajax mysql在线即时聊天源码 135
    04 wooyes.com 成功-我直接得 105
    05 内容类网站也就没有任何收入来源了 94
    06 祝大家蛇年快乐!! 92
    07 攒粒是什么?怎么用攒粒赚钱? 89
    08 360旗下纳米AI搜索,送百辆新能源汽车回馈活动进行中! 84
    09 番茄畅听:大厂旗下解放双手的听音频赚钱神器! 83
    10 在Ubuntu/Debian上一键安装kangle web server 82
    推薦主機