Open
Description
这是什么方面的想法?
简码功能
这个想法具体是要实现什么?
免插件实现隐藏内容回复后可见的功能
这个想法实现后有什么帮助?
有些文章或者有些分享的内容可能会希望隐藏某一部分,需求回复/登录后再给访客展示,可以通过简码的形式直接在主题内支持
是否有具体的实现思路?
根据网上的wp隐藏内容回复后看见的代码修改优化得到以下版本,对登录用户显示,对访客检查是否评论
function reply_to_read( $atts , $content = null ) {
extract(shortcode_atts( array(
"notice" => '<p class="reply-to-read" style="text-align:center; border:2px solid #f00; border-style:dotted; border-radius:4px; padding:5px; margin:10px;">
<strong style="color: red;">温馨提示:</strong>此处内容需要您<strong><a href="#respond" title="点击进行评论">回复评论</a></strong>或<strong><a href="'.wp_login_url(get_permalink()).'" title="点击登录">登录</a></strong>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新">刷新!</a></strong></p>'
), $atts));
$user_ID = (int) wp_get_current_user()->ID;
// 如果用户已登录,直接显示内容
if ( $user_ID > 0 ) {
return do_shortcode( $content );
}
// 如果未登录,检查是否评论过
$email = isset($_COOKIE['comment_author_email_' . COOKIEHASH])
? str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH])
: null;
if ( empty($email) ) {
return $notice;
}
global $wpdb;
$post_id = get_the_ID();
$query = $wpdb->prepare(
"SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID` = %d AND `comment_approved` = '1' AND `comment_author_email` = %s LIMIT 1",
$post_id,
$email
);
if ( $wpdb->get_results($query) ) {
return do_shortcode($content);
} else {
return $notice;
}
}
add_shortcode('reply', 'reply_to_read');
补充信息:
需要的博主也可以自行添加到function.php中使用,顺便有没有佬看看sql查询拼接有没有安全漏洞