WordPress文章添加内容回复可见代码实现

分享个本站所用的一个回复可见内容的代码

网上有很多这样的教程,但是很多都已经用不了了,插件装多了网站还卡

以下食用:

将以下的代码添加到你主题目录的functions.php即可

//部分内容评论可见
add_filter('the_content', 'hide');
add_filter('comment_text','hide');

function hide($content) {

if (preg_match_all('/<!--hide start{?([\s\S]*?)}?-->([\s\S]*?)<!--hide end-->/i', $content, $matches)) {

$params = $matches[1][0];
$defaults = array('reply_to_this' => 'false');
$params = wp_parse_args($params, $defaults);

$stats = 'hide';

if ($params['reply_to_this'] == 'true') {

global $current_user;
get_currentuserinfo();

if ($current_user->ID) {
$email = $current_user->user_email;
} else if (isset($_COOKIE['comment_author_email_'.COOKIEHASH])) {
$email = $_COOKIE['comment_author_email_'.COOKIEHASH];
}

$ereg = "^[_\.a-z0-9]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,5}$";
if (eregi($ereg, $email)) {
global $wpdb;
global $id;
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'");
if ($comments) {
$stats = 'show';
}
}

$tip = __('<span class="vihide">抱歉,隐藏内容 <a href="#comments">回复</a> 后刷新可见</span>', 'hide');
} else {
if (isset($_COOKIE['comment_author_'.COOKIEHASH]) or current_user_can('level_0')) {
$stats = 'show';
}
$tip = __();
}

$hide_notice = $tip;
if ($stats == 'show') {
$content = str_replace($matches[0], $matches[2], $content);
} else {
$content = str_replace($matches[0], $hide_notice, $content);
}
}

return $content;
}

add_action('admin_footer', 'hide_footer_admin');

CSS样式

.vihide{display:inline-block;text-align:center;border: 2px dashed #ff6666;padding:8px;margin:10px auto;color:#FF6666;width:100%;}
.vihide a{color:#04a1ef}
.vihide a:hover{color:#ffb300}

代码调用

后台编辑文章的时候添加以下代码

<!--hide start{reply_to_this=true}-->隐藏内容<!--hide end-->
购买后下载不了源码或源码功能失效请联系小编,确认后可立即退款,请勿无理投诉
© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 共7条

请登录后发表评论

    • 头像PrinSoul0
    • 头像PrinSoul0
    • 头像PrinSoul0
    • 头像PrinSoul0