WordPress优化

修改配置

WordPress中许多不必要的功能可以通过修改配置文件的方式关闭。注意:在进行相关操作前务必备份好源文件!

  • 在 wp-config.php文件后添加下列内容
//WordPress自定义优化项。
define('WP_DEBUG', false);  //关闭DEBUG
define('AUTOSAVE_INTERVAL', 86400);  //关闭自动保存
define('WP_POST_REVISIONS', false);  //关闭修订
define('DISALLOW_FILE_EDIT', true);  //关闭文件修改
define('FS_CHMOD_DIR', (0755 & ~ umask()));  //目录权限755
define('FS_CHMOD_FILE', (0644 & ~ umask()));  //文件权限644
 
//禁止更新(不建议)
//define('AUTOMATIC_UPDATER_DISABLED', true);
//define('WP_AUTO_UPDATE_CORE', false);
  • 当前主题目录中function.php后添加下列内容
//WordPress主题 functions.php 优化项
//引入方式:在主题functions.php后添加
 
//移除不必要的信息,如WordPress版本
remove_action('wp_head', 'feed_links', 2);  //移除feed
remove_action('wp_head', 'feed_links_extra', 3);  //移除feed
remove_action('wp_head', 'rest_output_link_wp_head', 10); 
remove_action('wp_head', 'rsd_link');  //移除离线编辑器开放接口
remove_action('wp_head', 'wlwmanifest_link');  //移除离线编辑器开放接口
remove_action('wp_head', 'index_rel_link');  //去除本页唯一链接信息
remove_action('wp_head', 'parent_post_rel_link', 10, 0);  //清除前后文信息
remove_action('wp_head', 'start_post_rel_link', 10, 0);  //清除前后文信息
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'locale_stylesheet');
remove_action('publish_future_post','check_and_publish_future_post',10, 1);
remove_action('wp_head', 'noindex', 1);
remove_action('wp_head', 'wp_print_styles', 8);  //载入css
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_head', 'wp_generator');  //移除WordPress版本
remove_action('wp_head', 'rel_canonical');
remove_action('wp_footer', 'wp_print_footer_scripts');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('template_redirect', 'wp_shortlink_header', 11, 0);
add_action('widgets_init', 'my_remove_recent_comments_style');
function my_remove_recent_comments_style() {
    global $wp_widget_factory;
    remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'] ,'recent_comments_style'));
}
 
//评论框禁止HTML
add_filter('pre_comment_content', 'wp_specialchars');
 
//停用链接猜测
add_filter('redirect_canonical', 'stop_guessing');
function stop_guessing($url) {
    if (is_404()) {
        return false;
    }
    return $url;
}
 
//禁止自动保存
remove_filter('the_content', 'wptexturize');
remove_action('pre_post_update', 'wp_save_post_revision');
add_action('wp_print_scripts', 'disable_autosave');
function disable_autosave() {
    wp_deregister_script('autosave');
}
 
//禁用wp-embed.min.js
function disable_embeds_init() {
    /* @var WP $wp */
    global $wp;
 
    // Remove the embed query var.
    $wp->public_query_vars = array_diff( $wp->public_query_vars, array(
        'embed',
    ) );
 
    // Remove the REST API endpoint.
    remove_action( 'rest_api_init', 'wp_oembed_register_route' );
 
    // Turn off
    add_filter( 'embed_oembed_discover', '__return_false' );
 
    // Don't filter oEmbed results.
    remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
 
    // Remove oEmbed discovery links.
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
 
    // Remove oEmbed-specific JavaScript from the front-end and back-end.
    remove_action( 'wp_head', 'wp_oembed_add_host_js' );
    add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );
 
    // Remove all embeds rewrite rules.
    add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
}
 
add_action( 'init', 'disable_embeds_init', 9999 );
 
/**
 * Removes the 'wpembed' TinyMCE plugin.
 *
 * @since 1.0.0
 *
 * @param array $plugins List of TinyMCE plugins.
 * @return array The modified list.
 */
function disable_embeds_tiny_mce_plugin( $plugins ) {
    return array_diff( $plugins, array( 'wpembed' ) );
}
 
/**
 * Remove all rewrite rules related to embeds.
 *
 * @since 1.2.0
 *
 * @param array $rules WordPress rewrite rules.
 * @return array Rewrite rules without embeds rules.
 */
function disable_embeds_rewrites( $rules ) {
    foreach ( $rules as $rule => $rewrite ) {
        if ( false !== strpos( $rewrite, 'embed=true' ) ) {
            unset( $rules[ $rule ] );
        }
    }
 
    return $rules;
}
 
//Remove embeds rewrite rules on plugin activation.
function disable_embeds_remove_rewrite_rules() {
    add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'disable_embeds_remove_rewrite_rules' );