WordPress教程

WordPress 开发过程中最常用的主题优化代码

阿里云

去除加载的 css 和 js 后面的版本号:

  1. function _remove_script_version( $src ){
  2.     $parts = explode( '?', $src );
  3.     return $parts[0];
  4. }
  5. add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
  6. add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
  7. add_filter( 'pre_option_link_manager_enabled', '__return_true' );
也想出现在这里?联系我们
创客主机

去除 wp-embed 功能,加快网页打开速度:

  1. remove_action('rest_api_init', 'wp_oembed_register_route');
  2. remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
  3. remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10 );
  4. remove_filter('oembed_response_data',   'get_oembed_response_data_rich',  10, 4);
  5. remove_action('wp_head', 'wp_oembed_add_discovery_links');
  6. remove_action('wp_head', 'wp_oembed_add_host_js');

替换 WordPress jquery 源为 loli.net,加快网页打开速度:

  1. add_action( 'init', 'jquery_register' );
  2. function jquery_register() {
  3. if ( !is_admin() ) {
  4. wp_deregister_script( 'jquery' );
  5. wp_register_script( 'jquery', ( 'https://cdnjs.loli.net/ajax/libs/jquery/1.12.4/jquery.min.js' ), false, null, true );
  6. wp_enqueue_script( 'jquery' );
  7. }
  8. }

关闭 XML-RPC,提高安全性:

  1. add_filter( 'xmlrpc_enabled', '__return_false' );

关闭 RSD Links

  1. remove_action( 'wp_head', 'rsd_link' ) ;

关闭 wlwmanifest Link

  1. remove_action('wp_head', 'wlwmanifest_link');

关闭 RSD Link Tag:

  1. remove_action('wp_head', 'rsd_link');

关闭 RSS:

  1. function itsme_disable_feed() {
  2.   wp_die( __( 'No feed available, please visit the <a href="'. esc_url( home_url( '/' ) ) .'">homepage</a>!' ) );
  3. }
  4.  
  5. add_action('do_feed', 'itsme_disable_feed', 1);
  6. add_action('do_feed_rdf', 'itsme_disable_feed', 1);
  7. add_action('do_feed_rss', 'itsme_disable_feed', 1);
  8. add_action('do_feed_rss2', 'itsme_disable_feed', 1);
  9. add_action('do_feed_atom', 'itsme_disable_feed', 1);
  10. add_action('do_feed_rss2_comments', 'itsme_disable_feed', 1);
  11. add_action('do_feed_atom_comments', 'itsme_disable_feed', 1);
  12. remove_action( 'wp_head', 'feed_links_extra', 3 );
  13. remove_action( 'wp_head', 'feed_links', 2 );

关闭 pingback,提升速度:

  1. function no_self_ping( &$links ) {
  2. $home = get_option( 'home' );
  3. foreach ( $links as $l => $link )
  4.   if ( 0 === strpos( $link, $home ) )
  5.     unset($links[$l]);
  6. }
  7. add_action( 'pre_ping', 'no_self_ping' );

移除 API Links

  1. add_action('after_setup_theme', function(){
  2.     remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
  3. });

移除 dashicons

  1. function wpdocs_dequeue_dashicon() {
  2. if (current_user_can( 'update_core' )) {
  3.   return;
  4. }
  5. wp_deregister_style('dashicons');
  6. }
  7. add_action( 'wp_enqueue_scripts', 'wpdocs_dequeue_dashicon' );

移除头部 link 的 api.w.org 连接

  1. remove_action('template_redirect', 'rest_output_link_header', 11, 0);

禁止非中文评论

  1. function refused_spam_comments( $comment_data ) {  
  2. $pattern = '/[一-龥]/u';  
  3. if(!preg_match($pattern,$comment_data['comment_content'])) {  
  4. wp_die('评论必须含中文!');  
  5. }  
  6. return( $comment_data );  
  7. }  
  8. add_filter('preprocess_comment','refused_spam_comments');

关闭 wordpress 后台主题插件更新提示

  1. add_filter('pre_site_transient_update_core', create_function('$a', "return null;")); // 关闭核心提示
  2. add_filter('pre_site_transient_update_plugins', create_function('$a', "return null;")); // 关闭插件提示
  3. add_filter('pre_site_transient_update_themes', create_function('$a', "return null;")); // 关闭主题提示
  4. remove_action('admin_init', '_maybe_update_core'); // 禁止 WordPress 检查更新
  5. remove_action('admin_init', '_maybe_update_plugins'); // 禁止 WordPress 更新插件
  6. remove_action('admin_init', '_maybe_update_themes'); // 禁止 WordPress 更新主题

WordPress 开发过程中最常用的主题优化代码

已有 286 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!