<?php
/**
* 🛠️ CubeDesigns - WordPress Core Optimizations and Tweaks 🛡️
*
* This file contains functions designed to improve performance, enhance security
* through obfuscation (removing version numbers), and clean up unnecessary HTML
* output from WordPress core and common plugins.
*
* Some of the below function are from the Plugin: Meta Generator and Version Info Remover
* (https://wordpress.org/plugins/meta-generator-and-version-info-remover/)
*/
// ======================================================================
// | 🧹 HEADER & META TAG CLEANUP (Security & Performance) |
// ======================================================================
/************************************************************************
* Removes various unnecessary links and meta tags from the <head> section.
* @return void
***********************************************************************/
function cds_clean_the_header_outout() {
// 🔒 Security: Remove WP Generator Vesion from header and RSS feeds
remove_action( 'wp_head', 'wp_generator' );
add_filter( 'the_generator', '__return_false' );
// 🔗 Link/Meta Removal
remove_action( 'wp_head', 'wlwmanifest_link' ); // Remove link for Windows Live Writer (legacy)..
remove_action( 'wp_head', 'rsd_link' ); // Remove Really Simple Discovery Link (XML-RPC).
// 🔗 Shortlinks Cleanup: Removes the <link rel='shortlink'> tag and the HTTP header
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'template_redirect', 'wp_shortlink_header', 11);
// ➡️ Relational Links Cleanup (Index/Start/Prev/Next/Parent links)
remove_action( 'wp_head', 'index_rel_link' ); // Remove Link to Home Page.
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // Remove First Post Link.
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // Remove Parent Post Link.
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); // Remove Prev-Next Links.
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10 ); // Remove Prev-Next Links from Header.
// ➡️ Feed Links (category/tag RSS/Atom feed links). If you don’t use feeds, these are pointless.
remove_action( 'wp_head', 'feed_links', 2); // Primary feed links (e.g., /feed/).
remove_action( 'wp_head', 'feed_links_extra', 3 ); // Extra feed links (e.g., category feeds)
// 💨 REST API Discovery Link (Performance/Obfuscation)
// REST API still works, but no longer advertises its endpoint in the header.
remove_action( 'wp_head', 'rest_output_link_wp_head', 10);
// 🚫 oEmbed Discovery + Scripts (Performance)
// Prevents WordPress from advertising oEmbed endpoints and loading extra JS.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action( 'wp_head', 'wp_oembed_add_host_js');
// 🎨 Global Styles (Block Editor/Gutenberg)
// Prevents WP from injecting the large default block editor CSS file.
remove_action( 'wp_head', 'wp_enqueue_global_styles', 1);
// Also remove the corresponding body class
remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
}
add_action( 'after_setup_theme', 'cds_clean_the_header_outout' );
/************************************************************************
* Remove Emoji Styles and Scripts (Performance)
* Disables the loading of CSS and JavaScript used for native WordPress emojis.
* @return void
***********************************************************************/
function cds_disable_wp_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
// Remove WP Emoji DNS prefetch from document head
add_filter( 'emoji_svg_url', '__return_false' );
}
add_action( 'init', 'cds_disable_wp_emojis' );
/************************************************************************
* Remove comment-reply.min.js (Performance)
* Deregisters the script if threaded comments are not globally enabled.
* @return void
***********************************************************************/
function cds_remove_comment_reply_js() {
// Only deregister if not on a single post/page that allows comments
if ( ! is_singular() ) {
wp_deregister_script( 'comment-reply' );
}
}
add_action('init', 'cds_remove_comment_reply_js', 10);
/************************************************************************
* Remove jQuery Migrate Script (Performance & Stability)
* Improves loading speed and avoids potential console warnings by removing
* the compatibility layer for older jQuery code.
* @param WP_Scripts $scripts
* @return void
***********************************************************************/
function cds_remove_jquery_migrate( $scripts ) {
if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) {
$script = $scripts->registered['jquery'];
// Remove 'jquery-migrate' from the list of dependencies for 'jquery'
if ( $script->deps ) {
$script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) );
}
}
}
// Use 'wp_default_scripts' to modify dependencies early
add_action( 'wp_default_scripts', 'cds_remove_jquery_migrate' );
// ======================================================================
// | 🔌 PLUGIN GENERATOR REMOVAL (Obfuscation) |
// ======================================================================
/************************************************************************
* Remove Yoast SEO comments (HTML Cleanup & Obfuscation)
* Removes the '' comments.
* @return void
***********************************************************************/
function cbs_remove_yoast_seo_comments() {
if ( class_exists( 'WPSEO_Frontend' ) ) {
$instance = WPSEO_Frontend::get_instance();
// Modern Yoast versions (v14+) use a filter for debug markers
add_filter('wpseo_debug_markers', '__return_false', 9999);
// Legacy method for older Yoast versions
if ( method_exists( $instance, 'debug_mark') ) {
remove_action( 'wpseo_head', array( $instance, 'debug_mark' ), 2 );
}
}
}
add_action('template_redirect', 'cbs_remove_yoast_seo_comments', 9999);
/************************************************************************
* Remove WPML generator meta tag.
* @return void
***********************************************************************/
if ( !empty ( $GLOBALS['sitepress'] ) ) {
function cds_remove_wpml_generator() {
remove_action(
current_filter(),
array ( $GLOBALS['sitepress'], 'meta_generator_tag' )
);
}
add_action( 'wp_head', 'cds_remove_wpml_generator', 0 );
}
/************************************************************************
* Remove Slider Revolution generator meta tag.
***********************************************************************/
add_filter( 'revslider_meta_generator', '__return_empty_string' );
/************************************************************************
* Remove Visual Composer / WPBakery Page Builder generator meta tag.
* @return void
***********************************************************************/
function cds_remove_visual_composer_generator() {
if ( class_exists( 'Vc_Manager' ) || class_exists( 'Vc_Base' ) ) {
// Correct way to target the class method used for meta data
remove_action('wp_head', array(visual_composer(), 'addMetaData'));
}
}
add_action('init', 'cds_remove_visual_composer_generator', 100);
// ======================================================================
// | 🗑️ CACHING & VERSIONING CLEANUP |
// ======================================================================
/************************************************************************
* Removes Query Strings (version numbers) from script and style files.
* * This is crucial for efficient caching and CDN delivery, as many services
* won't cache assets with query strings.
* * ⚠️ NOTE on Cache-Busting: When this is active, you MUST manually clear
* your site and CDN cache after every core, theme, or plugin update to
* ensure visitors see the latest file versions.
* @param string $target_url
* @return string
***********************************************************************/
function cds_remove_appended_version_script_style( $target_url ) {
// Check for 'ver=' argument
if (strpos( $target_url, 'ver=' )) {
$target_url = remove_query_arg( 'ver', $target_url );
}
// Check for 'version=' argument
if (strpos( $target_url, 'version=' )) {
$target_url = remove_query_arg( 'version', $target_url );
}
return $target_url;
}
// Set to high priority (20000) to ensure other functions don't re-add the version.
// add_filter('style_loader_src', 'cds_remove_appended_version_script_style', 20000);
// add_filter('script_loader_src', 'cds_remove_appended_version_script_style', 20000);