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

优秀源码 无标签
0 523
GS
GS 自成一派 2024-02-19 20:54:52
Lv:27级

纯代码实现

在当前网站主题下的 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】

楼主签名:Admin.Gs 站长故事
回帖
回复列表

    遵守各国法律法规 严谨违规内容

    • QQ群:1140251126
    • Email:admin@admin.gs
    • 本站可以自由发布正规网站外链
    • 本站域名 admin.gs 其它皆为闲置域名
    本站热帖
    01 域名真的不行了,往年卖出去的又可以注册了 648
    02 携手特工、雷人组建了nice臻选 571
    03 码支付漏洞测试开源版,来测试有没有漏洞 562
    04 我还有6个域名。 456
    05 只秀不卖 455
    06 免费收费域名 455
    07 最新版弹幕播放器源码,带后台 452
    08 免费三年香港云服务器录制视频 422
    09 免费域名申请地址 408
    10 扯谈域名投资 372
    推荐主机