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

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

纯代码实现

在当前网站主题下的 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 李子树上用布包着一个鸟蛋挂在上面代表什么 479
    02 也许我是做网站最辛苦人那部分人 419
    03 腾讯的SSL证书也变成90天了 265
    04 网站高级认证页面源码(给博客加上安全认证) 201
    05 支付宝当面付打赏PHP源码 128
    06 网校课堂知识视频付费源码,全开源 127
    07 最新仿微信即时聊天源码,支持视频语音聊天 125
    08 被大佬论坛撤销链接了 123
    09 搭建一个公益短视频去水印网站,支持几十种平台 114
    10 给大家推荐免费领取pos机的平台 89
    推荐主机