wordpress优化代码大汇总

剩余内容请到博客查看:https://www.ysboke.cn/archives/100

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

  
  
  1. //WordPress 评论回复邮件通知代码
  2. function fanly_comment_mail_notify($comment_id) {
  3. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  4. $comment = get_comment($comment_id);
  5. $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  6. $spam_confirmed = $comment->comment_approved;
  7. if (($parent_id != '') && ($spam_confirmed != 'spam')) {
  8. $wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME']));
  9. $to = trim(get_comment($parent_id)->comment_author_email);
  10. $subject = trim(get_comment($parent_id)->comment_author) . ',您在 [' . $blogname . '] 中的留言有新的回复啦!';
  11. $message = '
  12.  
  13. > 您在 ' . $blogname . ' 的留言有回复啦!
  14. 您好, ' . trim(get_comment($parent_id)->comment_author) . '! 您发表在文章 《' . get_the_title($comment->comment_post_ID) . '》 的评论:
  15. ' . nl2br(strip_tags(get_comment($parent_id)->comment_content)) . '
  16. ' . trim($comment->comment_author) . ' 给您的回复如下:
  17. ' . nl2br(strip_tags($comment->comment_content)) . '
  18. 您可以点击 查看完整的回复內容,也欢迎再次光临 ' . $blogname . '。祝您生活愉快!
  19. (此邮件由系统自动发出,请勿直接回复!)
  20.  
  21. ';
  22. $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
  23. $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  24. wp_mail( $to, $subject, $message, $headers );
  25. }
  26. }
  27. add_action('comment_post', 'fanly_comment_mail_notify');

自动标签内链

  
  
  1. /* 自动为文章内的标签添加内链开始 */
  2. $match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接
  3. $match_num_to = 1; //一篇文章中同一个标签最多自动链接几次
  4. function tag_sort($a, $b){
  5.         if ( $a->name == $b->name ) return 0;
  6.         return ( strlen($a->name) > strlen($b->name) ) ? - 1 : 1;
  7. }
  8. function tag_link($content){
  9.         global $match_num_from,$match_num_to;
  10.                 $posttags = get_the_tags();
  11.                 if ($posttags) {
  12.                         usort($posttags, "tag_sort");
  13.                         foreach($posttags as $tag) {
  14.                                 $link = get_tag_link($tag->term_id);
  15.                                 $keyword = $tag->name;
  16.                                 $cleankeyword = stripslashes($keyword);
  17.                                 $url = "<a href=\"$link\" title=\"".str_replace( '%s',addcslashes($cleankeyword, '$'),__( 'View all posts in %s')). "\"";
  18.                                 $url .= ' target="_blank"';
  19.                                 $url .= ">".addcslashes($cleankeyword, '$'). "</a>";
  20.                                 $limit = rand($match_num_from,$match_num_to);
  21.                                 $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word. ')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
  22.                                 $content = preg_replace( '|(<img)(.*?)('.$ex_word. ')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
  23.                                 $cleankeyword = preg_quote($cleankeyword, '\'');
  24.                                 $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
  25.                                 $content = preg_replace($regEx,$url,$content,$limit);
  26.                                 $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
  27.                         }
  28.                 }
  29.         return $content;
  30. }
  31. add_filter( 'the_content', 'tag_link', 1);
  32. /* 自动为文章内的标签添加内链结束 *

自动压缩页面

  
  
  1. //压缩html代码
  2. function wp_compress_html(){
  3. function wp_compress_html_main ($buffer){
  4. $initial=strlen($buffer);
  5. $buffer=explode("", $buffer);
  6. $count=count ($buffer);
  7. for ($i = 0; $i <= $count; $i++){ if (stristr($buffer[$i], '')) {
  8. $buffer[$i]=(str_replace("", " ", $buffer[$i]));
  9. } else {
  10. $buffer[$i]=(str_replace("\t", " ", $buffer[$i]));
  11. $buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i]));
  12. $buffer[$i]=(str_replace("\n", "", $buffer[$i]));
  13. $buffer[$i]=(str_replace("\r", "", $buffer[$i]));
  14. while (stristr($buffer[$i], ' ')) {
  15. $buffer[$i]=(str_replace(" ", " ", $buffer[$i]));
  16. }
  17. }
  18. $buffer_out.=$buffer[$i];
  19. }
  20. $final=strlen($buffer_out);
  21. $savings=($initial-$final)/$initial*100;
  22. $savings=round($savings, 2);
  23. $buffer_out.="\n";
  24. return $buffer_out;
  25. }
  26. ob_start("wp_compress_html_main");
  27. }
  28. add_action('get_header', 'wp_compress_html');
  29. //代码高亮段html 不压缩
  30. function unCompress($content) {
  31. if(preg_match_all('/(crayon-|<\/pre>)/i', $content, $matches)) {
  32. $content = ''.$content;
  33. $content.= '';
  34. }
  35. return $content;
  36. }
  37. add_filter( "the_content", "unCompress");
剩余内容请到博客查看:https://www.ysboke.cn/archives/100
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值