WordPress移除(wp_head)头部不必要的元素

以下内容都在主题的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

<script type="text/javascript">
            window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2.3\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2.3\/svg\/","svgExt":".svg","source":{"concatemoji":"http:\/\/wordpress.cc\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.8"}};
            !function(a,b,c){function d(a){var b,c,d,e,f=String.fromCharCode;if(!k||!k.fillText)return!1;switch(k.clearRect(0,0,j.width,j.height),k.textBaseline="top",k.font="600 32px Arial",a){case"flag":return k.fillText(f(55356,56826,55356,56819),0,0),b=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,56826,8203,55356,56819),0,0),c=j.toDataURL(),b===c&&(k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447),0,0),b=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447),0,0),c=j.toDataURL(),b!==c);case"emoji4":return k.fillText(f(55358,56794,8205,9794,65039),0,0),d=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55358,56794,8203,9794,65039),0,0),e=j.toDataURL(),d!==e}return!1}function e(a){var c=b.createElement("script");c.src=a,c.defer=c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g,h,i,j=b.createElement("canvas"),k=j.getContext&&j.getContext("2d");for(i=Array("flag","emoji4"),c.supports={everything:!0,everythingExceptFlag:!0},h=0;h<i.length;h++)c.supports[i[h]]=d(i[h]),c.supports.everything=c.supports.everything&&c.supports[i[h]],"flag"!==i[h]&&(c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&c.supports[i[h]]);c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&!c.supports.flag,c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.everything||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
        </script>
        <style type="text/css">
img.wp-smiley,
img.emoji {
    display: inline !important;
    border: none !important;
    box-shadow: none !important;
    height: 1em !important;
    width: 1em !important;
    margin: 0 .07em !important;
    vertical-align: -0.1em !important;
    background: none !important;
    padding: 0 !important;
}
</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 );//文章和评论feed
remove_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 );

<?php /* Plugin Name: 多功能 WordPress 插件 Plugin URI: https://yourwebsite.com/plugins/multifunctional Description: 包含置顶、网页宠物、哀悼模式、禁止复制、弹幕等 20+ 功能的综合插件 Version: 1.0.0 Author: Your Name Author URI: https://yourwebsite.com License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: multifunctional-plugin Domain Path: /languages */ // 防止直接访问 if (!defined('ABSPATH')) { exit; } // 定义插件常量 define('MULTI_PLUGIN_DIR', plugin_dir_path(__FILE__)); define('MULTI_PLUGIN_URL', plugin_dir_url(__FILE__)); define('MULTI_PLUGIN_VERSION', '1.0.0'); // 全局设置存储键 $multi_plugin_options = array( 'mourning_date', 'background_image', 'announcement', 'marquee_content', 'watermark_text' ); // ------------------------------ // 1. 置顶功能 // ------------------------------ function multi_post_sticky_meta_box() { add_meta_box( 'post_sticky', '文章置顶', 'multi_post_sticky_callback', 'post', 'side', 'default' ); } add_action('add_meta_boxes', 'multi_post_sticky_meta_box'); function multi_post_sticky_callback($post) { $sticky = get_post_meta($post->ID, '_post_sticky', true); wp_nonce_field('post_sticky_nonce', 'post_sticky_nonce'); echo '<label><input type="checkbox" name="post_sticky" value="1" ' . checked(1, $sticky, false) . '> 置顶此文章</label>'; } function multi_post_sticky_save($post_id) { if (!isset($_POST['post_sticky_nonce']) || !wp_verify_nonce($_POST['post_sticky_nonce'], 'post_sticky_nonce')) { return; } if (isset($_POST['post_sticky'])) { update_post_meta($post_id, '_post_sticky', 1); } else { delete_post_meta($post_id, '_post_sticky'); } } add_action('save_post', 'multi_post_sticky_save'); // ------------------------------ // 2. 网页宠物 // ------------------------------ function multi_web_pet() { echo '<div id="web-pet" style="position:fixed;bottom:20px;right:20px;z-index:9999;">'; echo '<img src="' . MULTI_PLUGIN_URL . 'assets/pet.png" alt="网页宠物" width="80">'; echo '</div>'; } add_action('wp_footer', 'multi_web_pet'); // ------------------------------ // 3. 哀悼模式 // ------------------------------ function multi_mourning_mode() { $mourning_date = get_option('multi_mourning_date', '2025-04-29'); // 默认日期 if (date('Y-m-d') === $mourning_date) { echo '<style>html { filter: grayscale(100%); }</style>'; } } add_action('wp_head', 'multi_mourning_mode'); // ------------------------------ // 4. 禁止复制 & 查看源码 // ------------------------------ function multi_disable_copy_source() { // 禁止复制样式 echo '<style>body { user-select: none; -moz-user-select: none; -webkit-user-select: none; }</style>'; // 禁止查看源码脚本 echo '<script>document.addEventListener("keydown", function(e) { if ((e.ctrlKey && e.key === "u") || e.key === "F12" || e.keyCode === 123) { e.preventDefault(); } });</script>'; } add_action('wp_head', 'multi_disable_copy_source'); // ------------------------------ // 5. 弹幕功能 // ------------------------------ function multi_danmaku() { echo '<div id="danmaku-container"></div>'; echo '<script src="' . MULTI_PLUGIN_URL . 'assets/danmaku.js"></script>'; // 需自行添加弹幕逻辑脚本 } add_action('wp_footer', 'multi_danmaku'); // ------------------------------ // 6. WP 优化 // ------------------------------ function multi_wp_optimization() { // 移除冗余功能 remove_action('wp_head', 'rsd_link'); remove_action('wp_head', 'wlwmanifest_link'); remove_action('wp_head', 'wp_generator'); remove_action('wp_head', 'feed_links', 2); remove_action('wp_print_styles', 'print_emoji_styles'); } add_action('init', 'multi_wp_optimization'); // ------------------------------ // 7. 媒体分类 // ------------------------------ function multi_media_category() { register_taxonomy( 'media_category', 'attachment', array( 'label' => __('媒体分类', 'multifunctional-plugin'), 'hierarchical' => true, 'show_ui' => true, 'query_var' => true ) ); } add_action('init', 'multi_media_category'); // ------------------------------ // 8. 预加载首页 // ------------------------------ function multi_preload_homepage() { echo '<link rel="preload" href="' . home_url() . '" as="document">'; } add_action('wp_head', 'multi_preload_homepage'); // ------------------------------ // 9. 在线客服 & 手机客服 // ------------------------------ function multi_support_buttons() { // 通用客服按钮 echo '<div id="support-button" class="desktop-only">'; echo '<a href="https://your客服链接.com" target="_blank">在线客服</a>'; echo '</div>'; // 手机端专属客服按钮 echo '<div id="mobile-support" class="mobile-only">'; echo '<a href="tel:1234567890">手机客服</a>'; echo '</div>'; } add_action('wp_footer', 'multi_support_buttons'); // ------------------------------ // 10. 网站背景 & 公告 // ------------------------------ function multi_background_announcement() { // 背景图片 $bg_image = get_option('multi_background_image', MULTI_PLUGIN_URL . 'assets/bg.jpg'); echo '<style>body { background-image: url("' . esc_url($bg_image) . '"); }</style>'; // 公告栏 $announcement = get_option('multi_announcement', '欢迎访问我们的网站!'); echo '<div class="site-announcement">' . esc_html($announcement) . '</div>'; } add_action('wp_head', 'multi_background_announcement'); // ------------------------------ // 11. 水印功能 // ------------------------------ function multi_watermark() { $watermark = get_option('multi_watermark_text', '版权所有 © 你的网站'); echo '<style> body::after { content: "' . esc_attr($watermark) . '"; position: fixed; top: 50%; left: 50%; transform: rotate(-45deg) translate(-50%, -50%); opacity: 0.1; font-size: 80px; color: #000; pointer-events: none; } </style>'; } add_action('wp_head', 'multi_watermark'); // ------------------------------ // 12. 后台设置页面 // ------------------------------ function multi_plugin_settings_page() { add_options_page( '多功能插件设置', '多功能插件', 'manage_options', 'multi-plugin-settings', 'multi_settings_html' ); } add_action('admin_menu', 'multi_plugin_settings_page'); function multi_settings_html() { if (!current_user_can('manage_options')) { wp_die(__('你没有权限访问此页面。')); } if (isset($_POST['multi_plugin_save'])) { foreach ($multi_plugin_options as $option) { update_option('multi_' . $option, sanitize_text_field($_POST[$option])); } echo '<div class="updated"><p>设置已保存!</p></div>'; } $options = array(); foreach ($multi_plugin_options as $option) { $options[$option] = get_option('multi_' . $option, ''); } ?> <div class="wrap"> <h1>多功能插件设置</h1> <form method="post"> <table class="form-table"> <tr> <th>哀悼日期 (YYYY-MM-DD)</th> <td><input type="text" name="mourning_date" value="<?php echo esc_attr($options['mourning_date']); ?>"></td> </tr> <tr> <th>背景图片 URL</th> <td><input type="url" name="background_image" value="<?php echo esc_attr($options['background_image']); ?>"></td> </tr> <tr> <th>公告内容</th> <td><input type="text" name="announcement" value="<?php echo esc_attr($options['announcement']); ?>"></td> </tr> <tr> <th>跑马灯内容</th> <td><input type="text" name="marquee_content" value="<?php echo esc_attr($options['marquee_content']); ?>"></td> </tr> <tr> <th>水印文本</th> <td><input type="text" name="watermark_text" value="<?php echo esc_attr($options['watermark_text']); ?>"></td> </tr> </table> <p class="submit"> <input type="submit" name="multi_plugin_save" class="button button-primary" value="保存设置"> </p> </form> </div> <?php } // ------------------------------ // 插件激活时创建默认设置 // ------------------------------ function multi_plugin_activate() { foreach ($multi_plugin_options as $option) { $default = ($option === 'mourning_date') ? '2025-04-29' : ''; add_option('multi_' . $option, $default); } } register_activation_hook(__FILE__, 'multi_plugin_activate'); // ------------------------------ // 资源路径说明(需手动创建目录) // ------------------------------ /* 请在插件目录下创建以下文件夹和文件: - assets/ - pet.png (网页宠物图片) - bg.jpg (默认背景图片) - danmaku.js (弹幕逻辑脚本) - style.css (自定义样式) */
最新发布
05-01
<think>好的,用户想要查看多功能WordPress插件的代码实现和功能列表。首先,我需要确认用户的具体需求是什么。他们可能是在寻找一个综合性的插件,或者想自己开发一个多功能插件,需要参考现有的实现。 根据用户提到的引用内容,之前提到了Syntax Highlighting Code Block插件和Yoast SEO插件。用户可能希望了解这类插件的代码结构和功能模块。但用户需要的是多功能插件,可能需要整合多个功能的例子,比如SEO、安全、性能优化等。 接下来,我需要考虑如何展示插件的代码结构。通常,WordPress插件由主PHP文件、包含各种类的文件、JavaScript和CSS资源,以及语言包组成。可以给出一个简单的示例代码框架,说明插件的基本结构,比如使用add_action和add_filter来挂载功能。 然后,功能列表部分需要涵盖常见多功能插件的模块,比如SEO工具、安全防护、缓存加速、表单构建、页面构建器、统计分析、社交媒体集成、备份恢复、多语言支持等。每个功能模块需要简要描述其实现方式,比如使用WordPress的钩子或者自定义数据库表。 用户还可能关心如何扩展插件功能,所以需要提到使用过滤器(filters)和动作(actions)来增加模块化。同时,代码示例部分要符合规范,使用正确的PHP语法,并展示插件头部注释、初始化类和挂载钩子的示例。 另外,引用中提到的Syntax Highlighting Code Block插件使用了PHPJavaScript,这可能对用户有帮助,可以引用作为例子。但要注意用户需求是多功能插件,所以重点应放在整合多个功能上,而不是单一功能的插件。 最后,相关问题部分需要引导用户深入探讨,比如开发注意事项、功能扩展方法、代码结构优化等。确保回答结构清晰,步骤明确,帮助用户逐步理解如何实现和查看多功能插件的代码及功能。</think>### WordPress多功能插件代码实现及功能解析 一、插件基础结构示例 ```php /* Plugin Name: 多功能工具箱 Description: 集成SEO优化、安全防护、缓存加速等核心功能 Version: 1.0.0 Author: Your Name */ class MultiFunctionPlugin { public function __construct() { // 初始化各功能模块 add_action('init', [$this, 'init_seo_module']); add_action('wp_enqueue_scripts', [$this, 'load_assets']); add_filter('the_content', [$this, 'content_optimization']); } public function init_seo_module() { // SEO模块初始化 require_once plugin_dir_path(__FILE__) . 'modules/seo-module.php'; new SEO_Module(); } public function load_assets() { wp_enqueue_style('multi-plugin-style', plugins_url('assets/css/main.css', __FILE__)); wp_enqueue_script('multi-plugin-script', plugins_url('assets/js/main.js', __FILE__), ['jquery'], null, true); } // 更多功能方法... } new MultiFunctionPlugin(); ``` (典型的多功能插件采用模块化架构,通过类封装核心功能[^1]) 二、功能列表及实现方式 | 功能模块 | 实现方式 | 技术要点 | |----------------|--------------------------------------------------------------------------|-----------------------------| | 1. SEO优化 | 通过meta标签注入、sitemap生成、内容分析等 | 使用`wp_head`钩子注入meta数据 | | 2. 安全防护 | 登录验证强化、恶意请求过滤、文件监控 | 自定义`authenticate`过滤器 | | 3. 缓存加速 | 生成静态缓存文件、数据库查询优化 | 对象缓存API扩展 | | 4. 表单构建器 | 可视化编辑器+短代码生成 | 结合React/Vue前端框架 | | 5. 页面构建器 | 区块拖拽系统+实时预览 | WordPress区块API扩展 | | 6. 统计分析 | 集成Google Analytics API+本地访问日志分析 | 数据库定时任务优化 | | 7. 社交媒体 | 分享按钮自动注入、OpenGraph协议支持 | 内容过滤器处理 | | 8. 备份恢复 | 数据库定时导出+云存储集成 | WP-Cron调度任务 | | 9. 多语言支持 | .mo文件加载+字符串替换机制 | 调用load_textdomain()方法 | 三、扩展功能实现示例(缓存模块) ```php class Cache_Module { const CACHE_TIME = 3600; public function __construct() { add_action('save_post', [$this, 'clear_post_cache']); add_filter('the_content', [$this, 'cache_handler'], 0); } private function get_cache_key() { return 'page_'.md5($_SERVER['REQUEST_URI']); } public function cache_handler($content) { if (!is_singular()) return $content; $cache = get_transient($this->get_cache_key()); if ($cache) return $cache; ob_start(); apply_filters('the_content', $content); $html = ob_get_clean(); set_transient($this->get_cache_key(), $html, self::CACHE_TIME); return $html; } } ``` (此代码展示了基于Transients API实现的页面缓存机制[^1]) 四、推荐参考项目 1. Jetpack插件(200+功能模块):https://github.com/Automattic/jetpack 2. All-in-One SEO Pack:https://github.com/awesomemotive/all-in-one-seo-pack 3. Wordfence安全插件:https://github.com/wordfence/wordfence
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值