WordPress代码实现回复可见

/ 0评 / 2

启用多说插件回复后,不能立刻显示内容,如果用WordPress自带的评论模块即秒回秒可见。

开始贴代码,将以下代码添加到主题文件functions.php结尾 ?> 前:

<span class="token selector">// 设置回复可见
function reply_to_read($atts, $content=null)</span> <span class="token punctuation">{</span>   
        <span class="token function">extract</span><span class="token punctuation">(</span>shortcode_<span class="token function">atts</span><span class="token punctuation">(</span><span class="token function">array</span><span class="token punctuation">(</span><span class="token string">"notice"</span> => <span class="token string">'温馨提示: 此处内容需要评论本文后才能查看.'</span><span class="token punctuation">)</span>, $atts<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   
        $email = null<span class="token punctuation">;</span>   
        $user_ID = <span class="token punctuation">(</span>int<span class="token punctuation">)</span> wp_get_current_<span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span>->ID<span class="token selector">;   
        if ($user_ID > 0)</span> <span class="token punctuation">{</span>   
            $email = get_<span class="token function">userdata</span><span class="token punctuation">(</span>$user_ID<span class="token punctuation">)</span>->user_email<span class="token punctuation">;</span>   
            //对博主直接显示内容   
            $admin_email = <span class="token string">"***@**.com "</span><span class="token selector">; //博主Email   
            if ($email == $admin_email)</span> <span class="token punctuation">{</span>   
                return $content<span class="token punctuation">;</span>   
            <span class="token punctuation">}</span>   
        <span class="token punctuation">}</span> <span class="token selector">else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH]))</span> <span class="token punctuation">{</span>   
            $email = str_<span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'%40'</span>, <span class="token string">'@'</span>, $_COOKIE[<span class="token string">'comment_author_email_'</span> . COOKIEHASH]<span class="token punctuation">)</span><span class="token punctuation">;</span>   
        <span class="token punctuation">}</span> <span class="token selector">else</span> <span class="token punctuation">{</span>   
            return $notice<span class="token punctuation">;</span>   
        <span class="token punctuation">}</span>   
        <span class="token selector">if (empty($email))</span> <span class="token punctuation">{</span>   
            return $notice<span class="token punctuation">;</span>   
        <span class="token punctuation">}</span>   
        global $wpdb<span class="token punctuation">;</span>   
        $post_id = get_the_<span class="token function">ID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token selector">;   
        $query = "SELECT `comment_ID` FROM</span> <span class="token punctuation">{</span>$wpdb->comments<span class="token punctuation">}</span> <span class="token selector">WHERE `comment_post_ID`=</span><span class="token punctuation">{</span>$post_id<span class="token punctuation">}</span> <span class="token selector">and `comment_approved`='1' and `comment_author_email`='</span><span class="token punctuation">{</span>$email<span class="token punctuation">}</span>' LIMIT 1"<span class="token selector">;   
        if ($wpdb->get_results($query))</span> <span class="token punctuation">{</span>   
            return do_<span class="token function">shortcode</span><span class="token punctuation">(</span>$content<span class="token punctuation">)</span><span class="token punctuation">;</span>   
        <span class="token punctuation">}</span> <span class="token selector">else</span> <span class="token punctuation">{</span>   
            return $notice<span class="token punctuation">;</span>   
        <span class="token punctuation">}</span>   
    <span class="token punctuation">}</span>   
    
    add_<span class="token function">shortcode</span><span class="token punctuation">(</span><span class="token string">'reply'</span>, <span class="token string">'reply_to_read'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  

添加之后修改Emaill为管理员邮箱,如果你的网站使用了AJAX免刷新提交评论,需要修改第2行的提示文字,提示访客评论后刷新页面来查看隐藏内容。

写文章时加入以下代码(删除@)

[@reply]评论可见的内容[@/reply]

[@reply notice=<span class="token string">"内容回复可见"</span>][@/reply]

手懒的同志使用easy2hide插件也可以实现回复可见功能。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注