Wordpress 网站免插件性能优化指南

阿三同学
2025-08-13 / 0 评论 / 6 阅读 / 正在检测是否收录...

注意:每一段代码上方都有功能注释,复制到主题 functions.php 文件,按需自取!

//移除标头超链接 s.w.org
remove_action('wp_head','wp_resource_hints',2);

//移除文章和评论feed
remove_action( 'wp_head', 'feed_links', 2 );

//移除分类等feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); 

// 隐藏WordPress登录失败时的错误提示信息
function no_wordpress_errors(){
  return 'Something is Wrong.';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

//替换Gavatar头像地址
function get_ssl_avatar($avatar) {
    if (preg_match_all(
        '/(src|srcset)=["\']https?.*?\/avatar\/([^?]*)\?s=([\d]+)&([^"\']*)?["\']/i',
        $avatar,
        $matches
    ) > 0) {
        $url = 'https://cdn.sep.cc';
        $size = $matches[3][0];
        $vargs = array_pad(array(), count($matches[0]), array());
        for ($i = 1; $i < count($matches); $i++) {
            for ($j = 0; $j < count($matches[$i]); $j++) {
                $tmp = strtolower($matches[$i][$j]);
                $vargs[$j][] = $tmp;
                if ($tmp == 'src') {
                    $size = $matches[3][$j];
                }
            }
        }
        $buffers = array();
        foreach ($vargs as $varg) {
            $buffers[] = vsprintf(
            '%s="%s/avatar/%s?s=%s&%s"',
            array($varg[0], $url, $varg[1], $varg[2], $varg[3])
           );
        }
        return sprintf(
                '<img alt="avatar" %s class="avatar avatar-%s" height="%s" width="%s" />',
                implode(' ', $buffers), $size, $size, $size
            );
    } else {
        return false;
    }
}
add_filter('get_avatar', 'get_ssl_avatar');

//WordPress查看站点新窗口打开
add_action( 'admin_bar_menu', 'customize_my_wp_admin_bar', 80 );
function customize_my_wp_admin_bar( $wp_admin_bar ) {

    //获取view-site 节点以便修改
    $node = $wp_admin_bar->get_node('view-site');

    //修改打开方式
    $node->meta['target'] = '_blank';

    //更新节点
    $wp_admin_bar->add_node($node);

}
//禁用懒加载
add_filter('wp_lazy_loading_enabled', '__return_false');

/* 评论作者链接新窗口打开 */
function my_get_comment_author_link() {
$url = get_comment_author_url( $comment_ID );
$author = get_comment_author( $comment_ID );
if ( empty( $url ) || 'http://' == $url )
return $author;
else
return "<a target='_blank' href='$url' rel='external nofollow' class='url'>$author</a>";
}
add_filter('get_comment_author_link', 'my_get_comment_author_link');

//禁止后台谷歌字体
function coolwp_remove_open_sans_from_wp_core() {
    wp_deregister_style( 'open-sans' );
    wp_register_style( 'open-sans', false );
    wp_enqueue_style('open-sans','');
}
add_action( 'init', 'coolwp_remove_open_sans_from_wp_core' );

//文章内链接新窗口打开
function autoblank($text) {
    $return = str_replace('<a', '<a target="_blank"', $text);
    return $return;
}
add_filter('the_content', 'autoblank');

// 设置图片的默认显示方式
add_action( 'after_setup_theme', 'default_attachment_display_settings' );
function default_attachment_display_settings() {
    update_option( 'image_default_align', 'center' ); //居中显示
    update_option( 'image_default_link_type', ' file ' ); //连接到媒体文件本身
    update_option( 'image_default_size', 'full' ); //完整尺寸
}

// 移除 WordPress 加载的JS和CSS链接中的版本号
function wpdaxue_remove_cssjs_ver( $src ) {
    if( strpos( $src, 'ver=' ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );

// 给WordPress分类目录链接删除斜杠
function mx_untrailingslashit($string, $type_of_url) {
 if ( $type_of_url != 'page' )
 $string = untrailingslashit($string);
 return rtrim( $string, '/\\' );
}
add_filter('user_untrailingslashit', 'mx_untrailingslashit', 10, 2);

//屏蔽站点管理员邮箱定期验证功能
add_filter( 'admin_email_check_interval', '__return_false');

// Disable auto-embeds for WordPress >= v3.5
remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );

// 移除头部 wp-json 标签和 HTTP header 中的 link
remove_action('wp_head', 'rest_output_link_wp_head', 10 );
remove_action('template_redirect', 'rest_output_link_header', 11 );

// Disable XML-RPC functionality
add_filter('xmlrpc_enabled', '__return_false');

//移除后台界面右上角的帮助和显示
add_action('in_admin_header', function(){
    global $current_screen;
    $current_screen->remove_help_tabs();
});
add_action('in_admin_header', function(){
    add_filter('screen_options_show_screen', '__return_false');
    add_filter('hidden_columns', '__return_empty_array');
});

//移除页面头部的版本和服务发现相关代码
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);

//彻底关闭 pingback
add_filter('xmlrpc_methods',function($methods){
    $methods['pingback.ping'] = '__return_false';
    $methods['pingback.extensions.getPingbacks'] = '__return_false';
    return $methods;
});

//禁用 pingbacks, enclosures, trackbacks
remove_action( 'do_pings', 'do_all_pings', 10 );

//去掉 _encloseme 和 do_ping 操作。
remove_action( 'publish_post','_publish_post_hook',5 );

//移除后台隐私相关的页面
add_action('admin_menu', function(){
    remove_submenu_page('options-general.php', 'options-privacy.php');
    remove_submenu_page('tools.php', 'export-personal-data.php');
    remove_submenu_page('tools.php', 'erase-personal-data.php');
}, 11);

//WordPress 彻底移除后台“隐私”设置功能
add_filter( 'map_meta_cap', 'ds_disable_core_privacy_tools', 10, 2 );
remove_action( 'init', 'wp_schedule_delete_old_privacy_export_files' );
remove_action( 'wp_privacy_delete_old_export_files', 'wp_privacy_delete_old_export_files' );
function ds_disable_core_privacy_tools( $caps, $cap ) {
    switch ( $cap ) {
        case 'export_others_personal_data':
        case 'erase_others_personal_data':
        case 'manage_privacy_options':
        $caps[] = 'do_not_allow';
        break;
    }
    return $caps;
}

//后台提速
remove_action('admin_init', '_maybe_update_core');
remove_action('admin_init', '_maybe_update_plugins');
remove_action('admin_init', '_maybe_update_themes');
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');

//RSS feed更新频率
add_filter( 'rss_update_period', function() {return 'weekly';} );
add_filter( 'rss_update_frequency', function() {return '2';} );

//隐藏feed 版本号
function ludou_remove_wp_version() {
  return '';
}
add_filter('the_generator', 'ludou_remove_wp_version');
0

评论 (0)

取消