以下代码是JV必备的。对于优化网站各方面都非常有效:

  1. //add1-七牛或又拍云CDN加速/////////////////////////////////////////////////
  2. define('FocusCDNHost','http://www.inlojv.com');//wordpress网站网址
  3. define('FocusCDNRemote','http://inlowp.qiniudn.com');//cdn域名
  4. define('FocusCDNIncludes','wp-content,wp-includes,avatar');//设置加速目录
  5. define('FocusCDNExcludes','.php|.xml|.html|.po|.mo');//设置文件白名单
  6. define('FocusCDNRelative','');//Check this if you want to have links like <wp-content/abc.png> rewritten - i.e. without your blog's domain as prefix.
  7. function do_cdnrewrite_ob_start() {
  8. $rewriter = new FocusCDNRewriteWordpress();
  9. $rewriter->register_as_output_buffer();
  10. }
  11. add_action('template_redirect', 'do_cdnrewrite_ob_start');
  12. class FocusCDNRewriteWordpress extends FocusCDNRewrite
  13. {
  14. function __construct() {
  15. $excl_tmp = FocusCDNExcludes;
  16. $excludes = array_map('trim', explode('|', $excl_tmp));
  17. parent::__construct(
  18. FocusCDNHost,
  19. FocusCDNRemote,
  20. FocusCDNIncludes,
  21. $excludes,
  22. !!FocusCDNRelative
  23. );
  24. }
  25. public function register_as_output_buffer() {
  26. if ($this->blog_url != FocusCDNRemote) {
  27. ob_start(array(&$this, 'rewrite'));
  28. }
  29. }
  30. }
  31. class FocusCDNRewrite {
  32. var $blog_url    = null;
  33. var $cdn_url     = null;
  34. var $include_dirs   = null;
  35. var $excludes    = array();
  36. var $rootrelative   = false;
  37. function __construct($blog_url$cdn_url$include_dirsarray $excludes$root_relative) {
  38. $this->blog_url   = $blog_url;
  39. $this->cdn_url    = $cdn_url;
  40. $this->include_dirs  = $include_dirs;
  41. $this->excludes   = $excludes;
  42. $this->rootrelative  = $root_relative;
  43. }
  44. protected function exclude_single(&$match) {
  45. foreach ($this->excludes as $badword) {
  46. if (stristr($match$badword) != false) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. protected function rewrite_single(&$match) {
  53. if ($this->exclude_single($match[0])) {
  54. return $match[0];
  55. else {
  56. if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
  57. return str_replace($this->blog_url, $this->cdn_url, $match[0]);
  58. else {
  59. return $this->cdn_url . $match[0];
  60. }
  61. }
  62. }
  63. protected function include_dirs_to_pattern() {
  64. $input = explode(',', $this->include_dirs);
  65. if ($this->include_dirs == '' || count($input) < 1) {
  66. return 'wp\-content|wp\-includes';
  67. else {
  68. return implode('|', array_map('quotemeta', array_map('trim', $input)));
  69. }
  70. }
  71. public function rewrite(&$content) {
  72. $dirs = $this->include_dirs_to_pattern();
  73. $regex = '#(?<=[(\"\'])';
  74. $regex .= $this->rootrelative
  75. ? ('(?:'.quotemeta($this->blog_url).')?')
  76. : quotemeta($this->blog_url);
  77. $regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
  78. return preg_replace_callback($regex, array(&$this, 'rewrite_single'), $content);
  79. }
  80. }
  81. //add2/////////////////////////////////////////
  82. remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );
  83. //add3//////////////
  84. add_filter('xmlrpc_enabled', '__return_false');
  85. //add4//////////////
  86. add_action( 'template_redirect', 'rw_relative_urls' );
  87. function rw_relative_urls() {
  88. if ( is_feed() || get_query_var( 'sitemap' ) ) //判断是否为feed页面或者sitemap页面
  89. return;
  90. $filters = array(
  91. 'post_link',
  92. 'post_type_link',
  93. 'page_link',
  94. 'attachment_link',
  95. 'get_shortlink',
  96. 'post_type_archive_link',
  97. 'get_pagenum_link',
  98. 'get_comments_pagenum_link',
  99. 'term_link',
  100. 'search_link',
  101. 'day_link',
  102. 'month_link',
  103. 'year_link',
  104. );
  105. foreach ( $filters as $filter )
  106. {
  107. add_filter( $filter, 'wp_make_link_relative' );
  108. }
  109. }
  110. //add5-强制js底部加载//////////////////
  111. function ds_print_jquery_in_footer( &$scripts) {
  112. if ( ! is_admin() )
  113. $scripts->add_data( 'jquery', 'group', 1 );
  114. }
  115. add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' );
  116. //add7-优化header///////////////////////
  117. remove_action( 'wp_head', 'feed_links', 2 ); //移除feed
  118. remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除feed
  119. remove_action( 'wp_head', 'rsd_link' ); //移除离线编辑器开放接口
  120. remove_action( 'wp_head', 'wlwmanifest_link' );  //移除离线编辑器开放接口
  121. remove_action( 'wp_head', 'index_rel_link' );//去除本页唯一链接信息
  122. remove_action('wp_head', 'parent_post_rel_link', 10, 0 );//清除前后文信息
  123. remove_action('wp_head', 'start_post_rel_link', 10, 0 );//清除前后文信息
  124. remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本
  125. remove_action( 'wp_head', 'rel_canonical' );
  126. remove_action( 'wp_footer', 'wp_print_footer_scripts' );
  127. remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
  128. remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
  129. add_action('widgets_init', 'my_remove_recent_comments_style');
  130. function my_remove_recent_comments_style() {
  131. global $wp_widget_factory;
  132. remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'] ,'recent_comments_style'));
  133. }
  134. //add8-异常登录后邮件通知//////////////////////////////////////////
  135. /*****************************************************
  136. 函数名称:wp_login_failed_notify v1.0 by DH.huahua.
  137. 函数作用:有错误登录wp后台就会email通知博主
  138. ******************************************************/
  139. function wp_login_failed_notify()
  140. {
  141. date_default_timezone_set('PRC');
  142. $admin_email = get_bloginfo ('admin_email');
  143. $to = $admin_email;
  144. $subject = '网站登录错误警告';
  145. $message = '<p>网站(' . get_option("blogname") . ')有异常登录!</p>' .
  146. '<p>请确定是您自己的登录失误,以防别人攻击!登录信息如下:</p>' .
  147. '<p>登录名:' . $_POST['log'] . '<p>' .
  148. '<p>登录密码:' . $_POST['pwd'] .  '<p>' .
  149. '<p>登录时间:' . date("Y-m-d H:i:s") .  '<p>' .
  150. '<p>登录IP:' . $_SERVER['REMOTE_ADDR'] . '<p>';
  151. $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
  152. $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
  153. $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  154. wp_mail( $to$subject$message$headers );
  155. }
  156. add_action('wp_login_failed', 'wp_login_failed_notify');
  157. //add9-屏蔽全英文评论/////////////////////////
  158. function refused_spam_comments( $comment_data ){
  159. $pattern = '/[一-龥]/u';
  160. if(!preg_match($pattern,$comment_data['comment_content'])){
  161. err( "You should type some Chinese word (like \"你好\") in your comment to pass the spam-check, thanks for your patience!" );
  162. }
  163. return$comment_data );
  164. }
  165. add_filter('preprocess_comment','refused_spam_comments');
  166. ///add10-防止暴力破解//////////////////////////
  167. ////修改后需要用这个地址才能打开 http://www.inlojv.com/wp-login.php?access=您的密码/////////
  168. add_action('login_enqueue_scripts','cracker');
  169. function cracker(){
  170. if($_GET['access']!= '这里填你的密码') header('Location:http://这里改为你的域名/');
  171. }

JV加入主题functions.php的优化代码