这次开发主题的 SEO 功能时,需要在 wp_head 中添加一些自定义代码,但 wp_head 函数太讨厌的,加载了一大堆乱七八糟的东西,看代码很不方便,得想办法删除掉,
这一次的 WordPress 开发教程就教大家怎么删除 wp_head 多余的代码。
- 代码来源:CSDN
**注意:**我这里是为了方便开发主题才这么做的,如果你不知道你在做什么,不要自己添加哦。
背景图片
在 functions.php 的<?php 下添加以下代码:
/* * 头部多余URL清理,放主题(functions.php) */function disable_emojis() { remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本 remove_action( 'wp_head', 'rsd_link' ); //移除离线编辑器开放接口 remove_action( 'wp_head', 'wlwmanifest_link' ); //移除离线编辑器开放接口 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 ); //清除前后文信息 remove_action( 'wp_head', 'feed_links', 2 ); //移除feed remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除feed remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); //移除wp-json链 remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); //头部的JS代码 remove_action( 'wp_head', 'wp_print_styles', 8 ); //emoji载入css remove_action( 'wp_head', 'rel_canonical' ); //rel=canonical remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); //rel=shortlink add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 ); //头部加载DNS预获取(dns-prefetch)}add_action( 'init', 'disable_emojis' );//移除WordPress头部加载DNS预获取(dns-prefetch)function remove_dns_prefetch( $hints, $relation_type ) { if ( 'dns-prefetch' === $relation_type ) { return array_diff( wp_dependencies_unique_hosts(), $hints ); } return $hints;}保存后刷新下前台,看看代码,是不是干净了许多。
如果对于这些移除 wp_head 多余代码有更多想了解的,可以看看这篇 WordPress 开发教程哦: