1. 找主题吧首页
  2. 建站教程
  3. WordPress教程
  4. 经验技巧

WordPress纯代码添加回复可见内容

1、在主题根目录下的functions.php文件中添加以下代码:

//内容回复可见
function reply_to_read($atts, $content = null) {
 extract(shortcode_atts(array(
 "notice" => '<div class="reply">隐藏内容,回复可见“<a href="' . get_permalink() . '#respond" title="回复可见">回复本文</a>”后“<a href="javascript:window.location.reload();" title="刷新页面">刷新页面</a>”查看隐藏内容!</div>'
 ) , $atts));
 $email = null;
 $user_ID = (int)wp_get_current_user()->ID;
 if ($user_ID > 0) {
 $email = get_userdata($user_ID)->user_email;
 //站长可见
 $admin_email = get_bloginfo('admin_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 (emptyempty($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');

2、在编辑器中添加自定义按钮functions.php添加以下代码:

//添加HTML编辑器自定义快捷标签按钮
add_action('after_wp_tiny_mce', 'bolo_after_wp_tiny_mce');
function bolo_after_wp_tiny_mce($mce_settings) {
?>
<script type="text/javascript">
QTags.addButton( 'reply', '回复可见', '[reply]\n\n[/reply]\n', "" );
</script>

3、在主题根目录下的style.css文件中添加以下代码:

/*回复可见*/
.reply {
  padding: 10px;
  border: 1px #49aff9 dashed;
  background-color: #fafafa;
  color: #f40000;
  text-align: center;
  font-size: 14px;
}

调用代码:在文本编辑框可看到回复可见按钮,插入即可,或者输入[@reply]和[/reply],其中@删除掉。

WordPress纯代码添加回复可见内容

原创文章,作者:主题哥,如若转载,请注明出处:http://www.lovecger.com/wordpress-pure-code-add-reply-to-see.html

发表评论

登录后才能评论