WordPress 本身成为 oEmbed 提供者,此功能对会在站点上生成一个额外的 HTTP 请求来加载文件,这导致加载 wp-embed.min.js 到每个页面上随着时间的推移体积不断增加,对于绝大多数网站来讲都不用不到可以将其关闭,荒岛本次带来 WordPress 禁用 oEmbed 功能和 Rest API 优化网站性能的教程,只要将下面的代码添加到主题的「functions.php」文件即可。宝藏来自荒岛 - 一座藏有宝藏的小岛-https://x-imagine.com/wordpress-disable-oembed-and-rest-api.html
// 荒岛一座藏有宝藏的小岛 function disable_embeds_init() { global $wp; $wp->public_query_vars = array_diff( $wp->public_query_vars, array( '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_response_data', 'get_oembed_response_data_rich', 10, 4); remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 ); remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); remove_action( 'wp_head', 'wp_oembed_add_host_js' ); add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' ); add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' ); add_filter( 'embed_oembed_discover', '__return_false' ); } add_action( 'init', 'disable_embeds_init', 9999 ); function disable_embeds_tiny_mce_plugin( $plugins ) { return array_diff( $plugins, array( 'wpembed' ) ); } function disable_embeds_rewrites( $rules ) { foreach ( $rules as $rule => $rewrite ) { if ( false !== strpos( $rewrite, 'embed=true' ) ) { unset( $rules[ $rule ] ); } } return $rules; } 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' ); function disable_embeds_flush_rewrite_rules() { remove_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' ); flush_rewrite_rules(); } register_deactivation_hook( __FILE__, 'disable_embeds_flush_rewrite_rules' );宝藏来自荒岛 - 一座藏有宝藏的小岛-https://x-imagine.com/wordpress-disable-oembed-and-rest-api.html
请按 Ctrl+D 收藏荒岛分享给好友 如您发现本文件已经失效无法下载请联系站长修正
- 荒岛公众号
- 扫一扫关注
- 荒岛小程序
- 扫一扫关注
评论