11个functions函数加速wordpress
wordpress功能很完善,但有时我们可以精简wordpress功能,提高wordpress速度,如何实现呢?在当前主题的function.php文件加入下面的函数就可以了。
禁用Google谷歌字体
wordpress前端和后台都引用了Google字体,由于Google字体源被X,极大影响了网页的载入速度,因此最好禁用谷歌字体。
function wpsilo_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', 'wpsilo_remove_open_sans_from_wp_core' );
替换gravatar
评论系统和作者头像会用到gravatar,因为gravatar在国外,国内网站gravatar的加载速度也很慢,因此很有必要禁用,或者用v2ex的gravatar服务替换。
禁用gravatar服务很简单,在wordpress后台=>设置=>讨论,最下面的头像设置中,不要勾选“显示头像”。
在functions.php中加入以下代码即可把gravatar头像替换为v2ex CDN
/*gravatar替换为v2ex的Gravatar CDN*/ function getV2exAvatar($avatar) { $avatar = str_replace(array("www.gravatar.com/avatar","0.gravatar.com/avatar","1.gravatar.com/avatar","2.gravatar.com/avatar"),"cdn.v2ex.com/gravatar",$avatar); return $avatar; } add_filter('get_avatar', 'getV2exAvatar');
或者直接可以禁用
WordPress 禁用 Gravatar 头像。
首先进入站点后台,打开 “设置” → “讨论”,向下滚动,找到 “头像” 设置区域,把 “显示头像” 勾选掉。
这样就不会显示 Gravatar 头像了,不过某些主题可能会导致样式错位。
强制jQuery库文件底部载入
//强制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' );
去除加载js和css后面的版本号
//去除加载的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 头不需要的代码
有效精简 WP 多余的nearing
//删除 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表情
WP 的 EMOJI 图片国内无法使用,直接禁用
// 禁用 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');
屏蔽文章 Embed 功能
//屏蔽文章 Embed 功能 remove_action('rest_api_init', 'wp_oembed_register_route'); remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4); remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10 ); remove_filter('oembed_response_data', 'get_oembed_response_data_rich', 10, 4); remove_action('wp_head', 'wp_oembed_add_discovery_links'); remove_action('wp_head', 'wp_oembed_add_host_js');
关闭 XML-RPC 功能
// 关闭 XML-RPC 功能 add_filter('xmlrpc_enabled', '__return_false');
屏蔽 REST API
如果你在使用微信小程序或者百度小程序,最好不要禁用rest-api功能,可能小程序依赖rest-api功能获取数据。
// 屏蔽 REST API add_filter('rest_enabled', '__return_false'); add_filter('rest_jsonp_enabled', '__return_false');
移除头部 wp-json 标签和 HTTP header 中的 link
//移除头部 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 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' );
参考文章https://www.wpsilo.com/wordpress-funcitons-optimize.html

加微信交流