2017年5月

待人友善是修养,独来独往是性格。

平常不喜欢和人有冲突,尽量避免不必要的争执,和气生财,以和为贵是我一直秉承的处世之道,除非触及我的底线。

要说独来独往,是因为我有太多的事情要做,父母、子女需要关心,工作、学习需要操心,真正知心的朋友也就几个,有些人情交际,没有必要也没有精力去参加。

可能,活了这么久,第一次真正不再强迫自己!

优先选择你手动设置的图像,如果未手动设置,自动将文章中的第一张图片设置为特色图像。

Function代码

//自动将文章中的第一张图片设置为特色图像,by minirizhi.com
function autoset_featured() {
    global $post;
    $already_has_thumb = has_post_thumbnail($post->ID);
        if (!$already_has_thumb)  {
        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
            if ($attached_image) {
                foreach ($attached_image as $attachment_id => $attachment) {
                set_post_thumbnail($post->ID, $attachment_id);
                }
            }
        }
 }
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');

评论者链接重定向,新窗口打开,并且添加nofollow属性。需要结合《Wordpress内链转外链》使用。

Function代码

//评论者链接重定向,by minirizhi.com
function get_comment_author_link_new($return, $author, $comment_ID = 0) {
    $comment = get_comment( $comment_ID );
    $url     = get_comment_author_url( $comment );
    $author  = get_comment_author( $comment );
 
    if ( empty( $url ) || 'http://' == $url )
        $return = $author;
    else
        if (!preg_match(home_url(),$url)) {
        $return = "<a href='".home_url()."/goto?url=".base64_encode($url)."' target='_blank' rel='external nofollow' class='url'>$author</a>";
        } else {
        $return = "<a href='$url' target='_blank'>$author</a>";
        }
    return $return;
}
add_filter('get_comment_author_link', 'get_comment_author_link_new', 10, 3);

文章外链转内链并转入跳转页面,需要两段代码:

Function代码

// 文章外链转内链并转入跳转页面,by minirizhi.com
add_filter('the_content','goto_url',999);
function goto_url($content){
    preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);
    if($matches){
        foreach($matches[2] as $val){
            if(strpos($val,'://')!==false && strpos($val,home_url())===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val) && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$val)){
                $content=str_replace("href=\"$val\"", "rel=\"nofollow\" target=\"_blank\" href=\"" . get_bloginfo('home'). "/goto?url=" .base64_encode($val). "\"",$content);
            }
        }
    }
    return $content;
}
// 下载外链跳转,by minirizhi.com
function links_nofollow($url) {
    if(strpos($url,'://')!==false && strpos($url,'echo.net')===false && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$url)) {
    $url = str_replace($url, home_url()."/goto?url=".base64_encode($url),$url);
         }
    return $url;
}

页面模版代码

<?php
    /*
    Template Name: 外链跳转
    Template Athor: minirizhi.com
    */
?>
<?php
$home = "https://minirizhi.com";    //修改成自己的网址
$name = "minirizhi";    //修改成自己的网站名
function be_referer($home,$name) {
    echo '<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8" /><meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>无效地址</title></head>
    <body>
    <div style="text-align:center;background:#eee;padding:50px 0;margin:0 auto">
    <p style="font-size:30px">抱歉,此URL不允许直接复制打开!!!</p>
    <p style="font-size:30px">—— 来自<a href="'.$home.'/">'.$name.'</a></p>
    </div>
    </body>
    </html>';
    exit();
}
// 判断:若不为跳转链接,则不执行跳转,显示以上信息
if(empty($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper($home)) === false){
    be_referer($home,$name);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<meta name="robots" content="noindex,nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="Cache-Control" content="no-transform"/>
<meta http-equiv="Cache-Control" content="no-siteapp"/>
<title>温馨提示:您即将离开 - <?php echo $name;?></title>
<style>
@media (max-width: 700px){body {font:12px!important}h2{font-size:18px!important;padding-bottom:10px!important;}.remind_title{font-size:16px!important;}a {font-size:14px!important;}.remind_detail {font-size:14px!important;}#wrapper {width:100%!important;}.c-container {padding: 0px!important;}}
body {font:15px  Arial, Lucida Grande, Tahoma, sans-serif;background: #fff;}
h2{font-size:22px;color:#eb5424;text-align:center;padding-bottom: 20px;border-bottom: 5px solid #dfdfdf;}
a {font-size:16px;color:#555;text-decoration: none;}
span {color:#CC0000;}
#wrapper {width:700px;margin:0 auto;}
#content {background:#fff;margin:10px 0 20px 0;}
.c-container {padding: 20px;}
.remind_icon {float: left;margin-right: 10px;display: inline;width: 32px;height: 32px;background: url('/content/themes/kavico/assets/img/prompt.png') no-repeat scroll 0% 0% transparent;background-position:-64px 0px;}
.remind_content {overflow: hidden;}
.remind_title {color:#D68300;margin-bottom: 10px;padding-top: 3px;font-weight: bold;font-size: 20px;font-family: "Microsoft YaHei","lucida Grande",Verdana;}
.remind_detail {line-height:1.5;font-size:15px;color: #535353;}
.safety-url {margin-bottom:15px;padding-bottom: 15px;border-bottom: 1px solid #DFDFDF;word-wrap: break-word;word-break: break-all;}
.c-footer {padding: 10px 15px;background: none repeat scroll 0% 0% #F1F1F1;border-top: 1px solid #BBB;overflow: hidden;text-align:center;}
a.btn_blue {border: 1px solid #0D659B;color: #FFF !important;background:linear-gradient(105deg,#238aca,#0074bc);display: inline-block;padding: 6px 25px;margin: 0px;font-size: 14px;font-weight: bold;text-align: center;border-radius: 3px;}
.c-footer-a2 {margin: 8px 0px 0px 15px;}
</style>
</head>
<body>
<div id="wrapper">
    <h2><a href="https://minirizhi.com" title="minirizhi">minirizhi</h2>/*修改成您的*/
    <div id="content">
    <div class="c-container">
        <span class="remind_icon"></span>
        <div class="remind_content">
            <div class="remind_title">您将要访问:</div>
            <div class="remind_detail">
                <div class="safety-url">
                    <span ><a href="<?php $url=$_GET['url']; echo base64_decode($url);?>" rel="nofollow" ><?php $url=$_GET['url']; echo base64_decode($url);?></a></span><br/>             
                </div>
                <p>该网页不属于余味记,我们无法确认该网页是否安全,它可能包含未知的安全隐患,请注意保护好个人信息! </p>
            </div>
        </div>
    </div>
    <div class="c-footer">
        <a href="<?php $url=$_GET['url']; echo base64_decode($url);?>" rel="nofollow" class="c-footer-a1 btn_blue">继续访问</a>
        <a href="https://minirizhi.com" class="c-footer-a2">返回 minirizhi</a>/*修改成您的*/
                
    </div>
    </div>
</div>
</body>
</html>

新建goto.php文件,填入页面模版代码,丢入模版文件夹。然后控制后台新建跳转链接页面,内容为空,slug名为goto,选择页面模版,发布即可。

Wordpress纯代码压缩输出内容,提升网页打开速度。

Function代码

//压缩html代码,by minirizhi.com 
function wp_compress_html(){
    function wp_compress_html_main ($buffer){
        $initial=strlen($buffer);
        $buffer=explode("<!--wp-compress-html-->", $buffer);
        $count=count ($buffer);
        for ($i = 0; $i <= $count; $i++){
            if (stristr($buffer[$i], '<!--wp-compress-html no compression-->')) {
                $buffer[$i]=(str_replace("<!--wp-compress-html no compression-->", " ", $buffer[$i]));
            } else {
                $buffer[$i]=(str_replace("\t", " ", $buffer[$i]));
                $buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i]));
                $buffer[$i]=(str_replace("\n", "", $buffer[$i]));
                $buffer[$i]=(str_replace("\r", "", $buffer[$i]));
                while (stristr($buffer[$i], '  ')) {
                    $buffer[$i]=(str_replace("  ", " ", $buffer[$i]));
                }
            }
            $buffer_out.=$buffer[$i];
        }
        $final=strlen($buffer_out);   
        $savings=($initial-$final)/$initial*100;   
        $savings=round($savings, 2);   
        $buffer_out.="\n<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->";   
    return $buffer_out;
}
if ( !is_admin() ) { 
        ob_start("wp_compress_html_main");
    }
}
add_action('init', 'wp_compress_html');
//文章代码不压缩
function unCompress($content) {
    if(preg_match_all('/(pre|<\/pre>)/i', $content, $matches)) {
        $content = '<!--wp-compress-html--><!--wp-compress-html no compression-->'.$content;
        $content.= '<!--wp-compress-html no compression--><!--wp-compress-html-->';
    }
    return $content;
}
add_filter( "the_content", "unCompress");

特别提示

1、<pre>代码内容</pre>之间的代码不会被压缩。

2、<!--wp-compress-html--><!--wp-compress-html no compression-->内容<!--wp-compress-html no compression--><!--wp-compress-html-->之间的内容不会被压缩。

3、你也可根据自己网站的情况,更改不被压缩的内容。

Wordpress评论框是默认是取消验证的,所以提交以后才提示内容为空,下面代码可以去除评论框novalidate属性,让空白内容无法提交!

Function代码

//去除评论框novalidate,by minirizhi.com
if(!is_admin()){
       ob_start("novalidate");
     }
function novalidate($buffer){
    $buffer= str_replace('novalidate','', $buffer);
    return $buffer;
}