记录一些常用的函数和瞎折腾的过程……

wordpress设置回复可见

提醒:本文最后更新于 2354 天前,文中所描述的信息可能已发生改变,请谨慎使用。

自己辛辛苦苦写了一些博客,不想让网友随意拿走,那么可以用回复可见这个功能。将下面的这个函数加到主题的functions.php中,对需要回复可见的内容部分这样使用:{reply}回复可见内容{/reply}(注:使用时将{ }替换成[ ]),这个功能就可以完美实现了!

//回复可见
function reply_to_read($atts, $content=null) {
    extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="color:red;">温馨提示:此处内容需要<b><a href="#respond" title="评论本文">评论本文</a></b>后才能查看!(评论后请刷新即可查阅!)</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 = "your@email.net"; //博主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');

在后台编辑器设置回复可见的按钮,实现对文字区域的隐藏,需要回复才可以看到见容:

//回复可见按钮
<?php
add_action('after_wp_tiny_mce', 'my_reply');
function my_reply($mce_settings) {
    if (wp_script_is('quicktags')){
?>
    <script type="text/javascript">
        QTags.addButton( 'myreply', '回复可见', "[reply]\n\n[/reply]", "" );
    </script>
<?php
    }
}