折腾下博客,一次失败的经历关于wordpress优化

PS:修改到最后还是放弃了,说实话真的受不了

增加回复可见

打开当前主题的functions.php自定一个函数,用法标签replyPS:修改到最后还是放弃了,说实话真的受不了,写的真心看不懂啊.以前的主题写的很正规的,现在是真心乱,放弃了,到处都是问题.

/*文章评论后显示隐藏内容代码*/
function reply_to_read($atts, $content=null) {   
        extract(shortcode_atts(array("notice" => '<p class="reply-to-read"><font color="#ff0000">温馨提示: </font>此处为隐藏内容,需要<a href="#respond" title="评论本文">评论本文</a>后才能查看.</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 = "xxx@aaa.com"; //博主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_author_email`='{$email}' LIMIT 1";   
        if ($wpdb->get_results($query)) {   
            return do_shortcode($content);   
        } else {   
            return $notice;   
        }   
    }   
    
    add_shortcode('reply', 'reply_to_read');

记得把上面邮箱改为博主邮箱,然后你不需要评论也能看
在写文章时只要在需要隐藏的内容前后加上

抱歉,只有登录并在本文发表评论才能阅读隐藏内容,切记不要恶意刷,否则会被限制,先阅读用户规则,一旦进入黑名单,不可能再放出来。同时注意,暂停在线支付,请联系客服QQ2441105221。

就可实现回复可见(*号要去掉)如:

抱歉,只有登录并在本文发表评论才能阅读隐藏内容,切记不要恶意刷,否则会被限制,先阅读用户规则,一旦进入黑名单,不可能再放出来。同时注意,暂停在线支付,请联系客服QQ2441105221。

也可以自定义隐藏提示信息,方法如下:

抱歉,只有登录并在本文发表评论才能阅读隐藏内容,切记不要恶意刷,否则会被限制,先阅读用户规则,一旦进入黑名单,不可能再放出来。同时注意,暂停在线支付,请联系客服QQ2441105221。

评论小工具美化

参考大佬的博客,网址忘记了,后补一下。

在主题目录文件夹下的theme-widgets.php  ,2.0在主题inc文件夹下widgets.php文件适当位置增加以下代码

class kratos_widget_comments extends WP_Widget {
    function __construct() {
        $widget_ops = array(
            'classname'  => 'widget_kratos_comments',
            'name'       => __('最近评论','moedog'),
            'description'=> __('Kratos主题特色组件 - 最近评论','moedog')
        );
        parent::__construct(false,false,$widget_ops);
    }
    function widget($args,$instance){
        if(!isset($args['widget_id'])) $args['widget_id'] = $this->id;
        $output = '';
        $title = isset($instance['title'])?$instance['title']:'最近评论';
        $number = isset($instance['number'])?absint($instance['number']):5;
        $show_admin = !empty($instance['show_admin'])?'1':'0';
        $comments = get_comments(apply_filters('widget_comments_args',array(
            'number' => $number,
            'author__not_in' => $show_admin,
            'status' => 'approve',
            'type' => 'comment',
            'post_status' => 'publish'
            )));
        $output    = $args['before_widget'];
        if($title) $output .= $args['before_title'].$title.$args['after_title'];
        $output .= '<div class="recentcomments">';
        if(is_array($comments)&&$comments){
            foreach($comments as $comment){
                $output .= '<li class="comment-listitem">';
                $output .= '<div class="comment-user">';
                $output .= '<span class="comment-avatar">'.get_avatar($comment,50,null).'</span>';
                $output .= '<div class="comment-author" title="'.$comment->comment_author.'">'.$comment->comment_author.'</div>';
                $output .= '<span class="comment-date">'.timeago($comment->comment_date_gmt).'</span>';
                $output .= '</div>';
                $output .= '<div class="comment-content-link"><a href="'.get_comment_link($comment->comment_ID).'"><div class="comment-content">'.convert_smilies(kratos_string_cut(strip_tags(get_comment_excerpt($comment->comment_ID)),30)).'</div></a></div>';
                $output .= '</li>';
            }

        }
        $output .= '</div>';
        $output .= $args['after_widget'];
        echo $output;
    }
    public function update($new_instance,$old_instance){
        $instance = $old_instance;
        $instance['title'] = sanitize_text_field($new_instance['title']);
        $instance['number'] = absint($new_instance['number']);
        $instance['show_admin'] = !empty($new_instance['show_admin'])?1:0;
        return $instance;
    }
    public function form($instance){
        $title = !empty($instance['title'])?$instance['title']:__('最近评论','moedog');
        $number = !empty($instance['number'])?absint($instance['number']):5;
        $show_admin = isset($instance['show_admin'])?(bool)$instance['show_admin']:false; ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:','moedog'); ?>
                <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
            </label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('显示数量:','moedog'); ?>
                <input class="tiny-text" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="number" step="1" min="1" max="99" value="<?php echo $number; ?>" size="3" />
            </label>
        </p>
        <p>
            <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_admin'); ?>" name="<?php echo $this->get_field_name('show_admin'); ?>"<?php checked($show_admin); ?> />
            <label for="<?php echo $this->get_field_id('show_admin'); ?>"><?php _e('不显示管理员(用户ID为1)评论','moedog'); ?></label>
        </p><?php
    }
}
 
//time ago
function timeago($ptime){
    $ptime = strtotime($ptime);
    $etime = time()-$ptime;
    if($etime<1) return __('刚刚','moedog');
    $interval = array(
        12*30*24*60*60 => __(' 年前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        30*24*60*60 => __(' 个月前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        7*24*60*60 => __(' 周前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        24*60*60 => __(' 天前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        60*60 => __(' 小时前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        60 => __(' 分钟前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
        1 => __(' 秒前','moedog').' ('.date(__('m月d日','moedog'),$ptime).')',
    );
    foreach($interval as $secs=>$str){
        $d=$etime/$secs;
        if($d>=1){
        $r=round($d);
        return$r.$str;
        }
    };
}
//string cut
function kratos_string_cut($string, $sublen, $start = 0, $code = 'UTF-8') {
    if($code == 'UTF-8') {
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
        preg_match_all($pa,$string,$t_string);
        if(count($t_string[0])-$start>$sublen) return join('',array_slice($t_string[0],$start,$sublen))."...";
        return join('',array_slice($t_string[0],$start,$sublen));
    }else{
        $start = $start*2;
        $sublen = $sublen*2;
        $strlen = strlen($string);
        $tmpstr = '';
        for($i=0;$i<$strlen;$i++){
            if($i>=$start&&$i<($start+$sublen)){
                if(ord(substr($string,$i,1))>129) $tmpstr .= substr($string,$i,2);
                else $tmpstr .= substr($string,$i,1);
            }
        if(ord(substr($string,$i,1))>129) $i++;
        }
        return $tmpstr;
    }
}

同样更加其他小工具找到合适的位置添加以下代码。
移除WP官方自带的最近评论工具,并注册相应小工具。在wiget.php里面找到相关代码,若无请添加。不会用的请点击这里

/*移除官方自带的自定义的小工具*/
unregister_widget('WP_Widget_Recent_Comments');
/*注册自定义的小工具*/
register_widget('kratos_widget_comments');

在主题目录下的 style.css 文件末尾添加以下样式代码

/** 最近评论小工具 **/
.widget_kratos_comments .recentcomments{padding:0 5px}
.widget_kratos_comments .comment-listitem{list-style:none;padding:8px 0!important}
.widget_kratos_comments .comment-user{font-size:13px}
.widget_kratos_comments .comment-avatar{float:left}
.widget_kratos_comments .comment-avatar img{float:left;margin-right:8px;width:50px;height:50px;border-radius:50%;-webkit-box-shadow:0 .125rem 1.0625rem 0 rgba(0,0,0,.1);box-shadow:0 .125rem 1.0625rem 0 rgba(0,0,0,.1)}
.widget_kratos_comments .comment-author{float:left;margin-right:10px;text-overflow:ellipsis;white-space:nowrap;overflow:hidden;max-width:95px}
.widget_kratos_comments .comment-content{font-size:14px;text-overflow:ellipsis;white-space:nowrap;overflow:hidden;width:75%}
.widget_kratos_comments .wp-smiley{max-height:20px !important}

最后在网站后台小工具里启用。

修改友情链接样式

参考wordpress 友情链接制作
新建Html文本小工具,填入以下代码

<div class="linkpage"><ul>
<li><a href="https://www.chinjua.com/" target="_blank"><img src="https://s2.ax1x.com/2020/02/18/3i5Eh4.png"><h4>天涯客</h4><p>80后,数学爱好者,电子书爱好者</p></a></li>
<li><a href="https://www.yeduk.com" target="_blank"><img src="https://img.hughlib.cn/wp-content/uploads/2020/04/favicon-1.png"><h4>夜读客</h4><p>夜读客</p></a></li>
<li><a href="https://www.azw3.tk" target="_blank"><img src="https://img.hughlib.cn/wp-content/uploads/2020/05/library.png"><h4>Kindle推送小站</h4><p>一个安静阅读的地方</p></a></li>
</ul></div>

添加CSS样式,在主题system.css下添加,忘记添加注释了。建议自己加上,以便以后修改。

.linkpage ul:after{content:" ";clear:both;display:block}
.linkpage li{float:left;width:48%;position:relative;transition:all .3s ease-out;border-radius:5px;line-height:1.3;height:50px;list-style:none; margin-bottom:20px;}
.linkpage h3{margin:15px -25px;padding:0 25px;border-left:5px solid #51aded;background-color:#f7f7f7;font-size:25px;line-height:40px}
.linkpage li:hover{background:rgba(230,244,250,.5);}
.linkpage li a{display:block;padding:0 10px 0 60px;height:50px;color:#555}
.linkpage li a img{width:50px!important;height:50px!important;border-radius:50%;position:absolute;top:2px;left:0px;}
.linkpage li a h4{color:#333;font-size:16px;margin:0 0 5px 0;transition:.5s;padding-top:2px}
.linkpage li a h4:hover{color:#51aded}
.linkpage li a h4,.linkpage li a p{white-space:nowrap;text-overflow:ellipsis;line-height:1.4}
.linkpage li a p{font-size:12px;color:#999;margin-top:5px;line-height:24px}
.linkpage ul{padding-left:0.1px}
.linkpage li{width:97%}

代码高亮

修改pre样式标签,实现代码高亮功能。点击这里下载源码,上传到到主题中的inc位置,例如我的wp-content/themes/Kratos-2/inc/code解压。或者按照下面操作

在主题文件夹inc目录下新建一个名为code的文件夹。
在code文件夹内再放入四个文件:code-button.php、editor-plugin.js、insert-code.php、tinymce.js 
在主题根目录下的/assets/css目录中放入prettify.css文件
在主题根目录下的/assets/js目录中放入prettify.js文件

将自动代码高亮选项加入到主题设置inc/theme-options.php

$options[] = array(
    'name' => '',
    'desc' => '自动代码高亮显示',
    'id' => 'gcp_code',
    'std' => '1',
    'type' => 'checkbox'
);
 
$options[] = array(
    'id' => 'clear'
);
 
$options[] = array(
    'name' => '',
    'desc' => '手动代码高亮显示',
    'id' => 'highlight',
    'std' => '1',
    'type' => 'checkbox'
);

调用自动高亮功能

// 自动代码高亮inc/theme-core.php
if (kratos_option('gcp_code',false)) {require get_template_directory() . '/inc/code/code-button.php';}

需要注意的是,当完成以上功能后,一定要检查原主题内是否有关于code或者<pre>标签的css样式加载,如果有需要去掉原有主题的css样式以防止覆盖了prettify.css

完成以上操作后,在【主题选项】中勾选【自动代码高亮】选项后就可以在编辑文章时使用该功能了。

  1. 山涧小石说道:

    测试一下发信,发现3.x版本真心BUG真多啊,懒得折腾了,准备晚上改回来

发表回复