wordpress优化代码大汇总

2020-09-25 1 189

评论回复自动邮件通知对方

//WordPress 评论回复邮件通知代码
function fanly_comment_mail_notify($comment_id) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME']));
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = trim(get_comment($parent_id)->comment_author) . ',您在 [' . $blogname . '] 中的留言有新的回复啦!';
$message = '

> 您在 ' . $blogname . ' 的留言有回复啦!
您好, ' . trim(get_comment($parent_id)->comment_author) . '! 您发表在文章 《' . get_the_title($comment->comment_post_ID) . '》 的评论:
' . nl2br(strip_tags(get_comment($parent_id)->comment_content)) . '
' . trim($comment->comment_author) . ' 给您的回复如下:
' . nl2br(strip_tags($comment->comment_content)) . '
您可以点击 查看完整的回复內容,也欢迎再次光临 ' . $blogname . '。祝您生活愉快!
(此邮件由系统自动发出,请勿直接回复!)

';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $message, $headers );
}
}
add_action('comment_post', 'fanly_comment_mail_notify');

自动标签内链

/* 自动为文章内的标签添加内链开始 */
$match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接
$match_num_to = 1; //一篇文章中同一个标签最多自动链接几次
function tag_sort($a, $b){
        if ( $a->name == $b->name ) return 0;
        return ( strlen($a->name) > strlen($b->name) ) ? - 1 : 1;
}
function tag_link($content){
        global $match_num_from,$match_num_to;
                $posttags = get_the_tags();
                if ($posttags) {
                        usort($posttags, "tag_sort");
                        foreach($posttags as $tag) {
                                $link = get_tag_link($tag->term_id);
                                $keyword = $tag->name;
                                $cleankeyword = stripslashes($keyword);
                                $url = "<a href=\"$link\" title=\"".str_replace( '%s',addcslashes($cleankeyword, '$'),__( 'View all posts in %s')). "\"";
                                $url .= ' target="_blank"';
                                $url .= ">".addcslashes($cleankeyword, '$'). "</a>";
                                $limit = rand($match_num_from,$match_num_to);
                                $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word. ')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
                                $content = preg_replace( '|(<img)(.*?)('.$ex_word. ')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
                                $cleankeyword = preg_quote($cleankeyword, '\'');
                                $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
                                $content = preg_replace($regEx,$url,$content,$limit);
                                $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
                        }
                }
        return $content;
}
add_filter( 'the_content', 'tag_link', 1);
/* 自动为文章内的标签添加内链结束 */

自动压缩页面

//压缩html代码
function wp_compress_html(){
function wp_compress_html_main ($buffer){
$initial=strlen($buffer);
$buffer=explode("", $buffer);
$count=count ($buffer);
for ($i = 0; $i <= $count; $i++){ if (stristr($buffer[$i], '')) {
$buffer[$i]=(str_replace("", " ", $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";
return $buffer_out;
}
ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');
//代码高亮段html 不压缩
function unCompress($content) {
if(preg_match_all('/(crayon-|<\/pre>)/i', $content, $matches)) {
$content = ''.$content;
$content.= '';
}
return $content;
}
add_filter( "the_content", "unCompress");

匿名

发表评论

匿名网友

评论:1   其中:访客  1   博主  0
    • 崩天的勾玉 崩天的勾玉

      666