壹影博客.
我在下午4点钟开始想你
wordpress速度优化教程
  • 2020-7-9日
  • 0评论
  • 894围观

wordpress速度优化教程

你在搭建WordPress博客时是否有这样的困惑,明明自己买的服务器速度很快,但是为什么进入wordpress后台的时候会很慢呢?

主要原因是因为wordpress后台进入时会加载一些官网预设的资源或是请求,因为wordpress官网以及请求地址都在国外,所以导致打开wordpress后台的时候会非常慢

那么我们对症下药,我们只需要把不需要的请求关闭即可

这里小编已经整理好了修改后的代码 ,直接复制粘贴到你的wordpress模板里面即可

后台优化代码

//加速配置
    //WordPress 后台禁用Google Open Sans字体,加速网站
    add_filter( 'gettext_with_context', 'wpdx_disable_open_sans', 888, 4 );
    function wpdx_disable_open_sans( $translations, $text, $context, $domain ) {
      if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
        $translations = 'off';
      }
      return $translations;
    }
    
    //使用 V2EX 的 Gravatar 镜像
    function get_ssl_avatar($avatar) {
       $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/','<img src="https://cdn.v2ex.co/gravatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">',$avatar);
       return $avatar;
    }
    add_filter('get_avatar', 'get_ssl_avatar');
    
    
    //强制jquery库文件底部载入
    function ds_print_jquery_in_footer( &$scripts) {
        if ( ! is_admin() )
            $scripts->add_data( 'jquery', 'group', 1 );
    }
    add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' );
    
    
    //去除加载的css和js后面的版本号
    function _remove_script_version( $src ){
        $parts = explode( '?', $src );
        return $parts[0];
    }
    add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
    add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
    add_filter( 'pre_option_link_manager_enabled', '__return_true' );
    
    
    //删除 wp_head 中无关紧要的代码
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wlwmanifest_link');
    remove_action('wp_head', 'wp_generator');
    remove_action('wp_head', 'start_post_rel_link');
    remove_action('wp_head', 'index_rel_link');
    remove_action('wp_head', 'adjacent_posts_rel_link');
    
    
    // 禁用 Emoji 功能 
    remove_action('admin_print_scripts',    'print_emoji_detection_script');
    remove_action('admin_print_styles', 'print_emoji_styles');
    #remove_action('wp_head',       'print_emoji_detection_script', 7);
    remove_action('wp_print_styles',    'print_emoji_styles');
    remove_action('embed_head',     'print_emoji_detection_script');
    remove_filter('the_content_feed',   'wp_staticize_emoji');
    remove_filter('comment_text_rss',   'wp_staticize_emoji');
    remove_filter('wp_mail',        'wp_staticize_emoji_for_email');
    
    
    // Disable auto-embeds for WordPress >= v3.5
    remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );
    add_filter('automatic_updater_disabled', '__return_true');  // 彻底关闭自动更新
    
    remove_action('init', 'wp_schedule_update_checks'); // 关闭更新检查定时作业
    wp_clear_scheduled_hook('wp_version_check');            // 移除已有的版本检查定时作业
    wp_clear_scheduled_hook('wp_update_plugins');       // 移除已有的插件更新定时作业
    wp_clear_scheduled_hook('wp_update_themes');            // 移除已有的主题更新定时作业
    wp_clear_scheduled_hook('wp_maybe_auto_update');        // 移除已有的自动更新定时作业
    
    remove_action( 'admin_init', '_maybe_update_core' );        // 移除后台内核更新检查
    
    remove_action( 'load-plugins.php', 'wp_update_plugins' );   // 移除后台插件更新检查
    remove_action( 'load-update.php', 'wp_update_plugins' );
    remove_action( 'load-update-core.php', 'wp_update_plugins' );
    remove_action( 'admin_init', '_maybe_update_plugins' );
    
    remove_action( 'load-themes.php', 'wp_update_themes' );     // 移除后台主题更新检查
    remove_action( 'load-update.php', 'wp_update_themes' );
    remove_action( 'load-update-core.php', 'wp_update_themes' );
    remove_action( 'admin_init', '_maybe_update_themes' );

//加速配置

123

发表评论