WordPress 网站<head>中默认有很多东西,有些可能不需要的,可以使用以下方法移除掉。每个部分的去处方法和说明已经备注。
//清洗header
// 移除REST API的链接标签
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
/**是一个用于从WordPress页面的 <head> 部分移除特定动作(action)的函数调用。
* 具体来说,这个调用会移除名为 wp_oembed_add_discovery_links 的函数,该函数负责在页面头部输出oEmbed发现链接。
* 这些链接使得其他网站可以更容易地嵌入你的内容。
* */
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
//移除名为 print_emoji_detection_script 的函数,该函数负责在页面头部输出检测和加载表情符号(emoji)的JavaScript脚本。。
remove_action( 'wp_print_styles', 'print_emoji_styles' );
//移除名为 print_emoji_styles 的函数,该函数负责在页面中输出表情符号(emoji)相关的CSS样式。
remove_action( 'wp_head', 'feed_links', 2 );
//移除 feed_links 动作,并且指定了优先级为2。
remove_action( 'wp_head', 'rsd_link' );
//移除 rsd_link 动作,默认优先级为10。
remove_action( 'wp_head', 'wlwmanifest_link' );
//移除 wlwmanifest_link 动作,默认优先级为10。
//remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
//移除后面文章的url,parent_post_rel_link()函数已经被弃用
//remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
//移除最开始文章的url,start_post_rel_link()函数已经被弃用
//remove_action('wp_head', 'index_rel_link');
//当前文章的索引,index_rel_link()函数已经被弃用
// remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
//自动生成的短链接
remove_action( 'wp_head', 'wp_generator' );
// 删除WordPress版本号。
remove_action('wp_head', 'feed_links_extra', 3);
// 移除名为 feed_links_extra 的函数,该函数负责在页面头部输出额外的RSS和Atom订阅链接,比如分类、标签等的订阅链接。
//remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
// 上、下篇,显示与当前帖子相邻的帖子的关系链接。
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
//rel=pre,显示单个帖子页面的当前帖子相邻的帖子的关系链接。
wp_deregister_script('l10n');
/**用于取消注册(deregister)特定脚本的WordPress函数。
* 这个函数通常用于移除已经通过 wp_register_script() 或 wp_enqueue_script() 注册或队列化的脚本。
* l10n 脚本通常是指本地化(localization)脚本,它包含了前端JavaScript代码所需的本地化数据。
* 这些数据通常包括翻译字符串、日期格式等,使得JavaScript代码能够根据用户的语言设置进行本地化。
* */
remove_filter('the_content', 'wptexturize');
//禁用半角符号自动转换为全角
remove_action('wp_head', 'wp_resource_hints', 2);
/**移除名为 wp_resource_hints 的函数,该函数负责在页面头部输出资源提示(Resource Hints),
* 如DNS预解析(DNS prefetch)、预加载(preload)、预连接(preconnect)和预取(prefetch)等。
* */
add_filter('json_enabled', '__return_false' );
add_filter('json_jsonp_enabled', '__return_false' );
// 禁用REST API
//add_filter('rest_authentication_errors', '__return_false');
/**这个过滤器用于处理REST API的认证错误。默认情况下,如果用户没有通过认证,REST API会返回一个401 Unauthorized状态码。
* 通过将这个过滤器设置为__return_false,你可以改变这一行为,使得即使用户未认证,请求也不会返回认证错误。
* 这通常不是推荐的做法,因为这样可能会导致安全问题,允许未认证用户访问本应受保护的数据。
* */
add_filter('rest_jsonp_enabled', '__return_false');
/**这个过滤器用于禁用JSONP(JSON with Padding)支持。
* JSONP是一种跨域请求的方法,允许从不同的域名加载数据。禁用JSONP可以提高安全性,因为它减少了跨站脚本攻击(XSS)的风险。
* */
/*
//删除已经排队的 CSS 样式表。不推荐
if ( ! function_exists( 'fanly_remove_styles_inline' ) ) :
function fanly_remove_styles_inline(){
// wp_dequeue_style( 'dashicons' );
// wp_dequeue_style( 'admin-bar' );
wp_dequeue_style( 'wp-block-library' );//块样式表
wp_dequeue_style( 'classic-theme-styles' );
wp_dequeue_style( 'global-styles' );
}
endif;
add_action('wp_enqueue_scripts', 'fanly_remove_styles_inline');
*/
//移除WordPress加载的JS和CSS链接中的版本号
if ( ! function_exists( 'wpdaxue_remove_cssjs_ver' ) ) :
function wpdaxue_remove_cssjs_ver( $src ) {
if( strpos( $src, 'ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
endif;
add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );//筛选排队样式的完全限定 URL。
add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );//筛选脚本加载程序源。

