栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > php开源框架 > wordpress

移除(wp_head)函数-头部不必要的元素标签

wordpress 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

  以下内容都在主题的functions.php文件写入



1.移除WordPress版本信息

remove_action( 'wp_head', 'wp_generator' );

2.移除离线编辑器开放接口

remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );

3.移除WordPress头部加载DNS预获取

<link rel='dns-prefetch' href='//s.w.org' />

移除代码

function remove_dns_prefetch( $hints, $relation_type ) {
    if ( 'dns-prefetch' === $relation_type ) {
        return array_diff( wp_dependencies_unique_hosts(), $hints );
    }

    return $hints;}add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );

4.移除emoji表情script和style

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

5.移除wp-json链接

<link rel='https://api.w.org/' href='http://wordpress.cc/wp-json/' />

移除

remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );

6.移除 wp-block-library-css 前端 CSS

function fanly_remove_block_library_css() {
    wp_dequeue_style( 'wp-block-library' );}add_action( 'wp_enqueue_scripts', 'fanly_remove_block_library_css', 100 );

7.移除前后文、第一篇文章、主页meta信息

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 );

8.移除评论

remove_action( 'wp_head', 'feed_links', 2 );//文章和评论feedremove_action( 'wp_head', 'feed_links_extra', 3 ); //分类等feed

9.移除wp-json api 功能

[link rel='https://api.w.org/' href='http://example.com/wp-json/' /][link rel="alternate" type="application/json+oembed" href="http://example.com/wp-json/oembed/1.0/embed?url=..." /][link rel="alternate" type="text/xml+oembed" href="http://example.com/wp-json/oembed/1.0/embed?url=..." /]

移除

remove_action( 'wp_head', 'rest_output_link_wp_head' );remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );

10.隐藏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 );

11.移除短链接shortlink

remove_action('wp_head','wp_shortlink_wp_head',10,0);

移除wp-json

// 屏蔽 REST API
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');// 移除头部 wp-json 标签和 HTTP header 中的 link remove_action('wp_head', 'rest_output_link_wp_head', 10 );remove_action('template_redirect', 'rest_output_link_header', 11 );


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/823139.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号