An advanced inc/core/cleanup.php for your RX Theme: WordPress head cleanup, performance cleanup, security hardening, emoji/oEmbed/XML-RPC tweaks, REST cleanup, comments cleanup, and safe defaults with filters so you can enable/disable parts later.
Below is an advanced cleanup.php file for your RX Theme. Put it here:
rx-theme/inc/core/cleanup.php
Then load it from functions.php like this:
require_once get_template_directory() . '/inc/core/cleanup.php';
inc/core/cleanup.php
<?php
/**
* RX Theme Cleanup
*
* Advanced WordPress cleanup, performance optimization, security hardening,
* head cleanup, REST cleanup, comments cleanup, emoji cleanup, oEmbed cleanup,
* feed cleanup, query cleanup, asset cleanup, and safe theme-level defaults.
*
* @package RX_Theme
*/
defined( 'ABSPATH' ) || exit;
if ( ! defined( 'RX_THEME_CLEANUP_VERSION' ) ) {
define( 'RX_THEME_CLEANUP_VERSION', '1.0.0' );
}
/**
* ------------------------------------------------------------
* Global Cleanup Settings
* ------------------------------------------------------------
*
* You can override these constants in wp-config.php before WordPress loads.
*
* Example:
* define( 'RX_DISABLE_EMOJIS', true );
* define( 'RX_DISABLE_XMLRPC', true );
*/
defined( 'RX_DISABLE_EMOJIS' ) || define( 'RX_DISABLE_EMOJIS', true );
defined( 'RX_DISABLE_EMBEDS' ) || define( 'RX_DISABLE_EMBEDS', true );
defined( 'RX_DISABLE_XMLRPC' ) || define( 'RX_DISABLE_XMLRPC', true );
defined( 'RX_DISABLE_RSD_LINK' ) || define( 'RX_DISABLE_RSD_LINK', true );
defined( 'RX_DISABLE_WLW_LINK' ) || define( 'RX_DISABLE_WLW_LINK', true );
defined( 'RX_DISABLE_SHORTLINK' ) || define( 'RX_DISABLE_SHORTLINK', true );
defined( 'RX_DISABLE_GENERATOR_TAG' ) || define( 'RX_DISABLE_GENERATOR_TAG', true );
defined( 'RX_DISABLE_REST_LINKS' ) || define( 'RX_DISABLE_REST_LINKS', false );
defined( 'RX_DISABLE_FEEDS' ) || define( 'RX_DISABLE_FEEDS', false );
defined( 'RX_DISABLE_COMMENTS_FRONTEND' ) || define( 'RX_DISABLE_COMMENTS_FRONTEND', false );
defined( 'RX_DISABLE_AUTHOR_ARCHIVES' ) || define( 'RX_DISABLE_AUTHOR_ARCHIVES', false );
defined( 'RX_DISABLE_ATTACHMENT_PAGES' ) || define( 'RX_DISABLE_ATTACHMENT_PAGES', true );
defined( 'RX_CLEANUP_QUERY_STRINGS' ) || define( 'RX_CLEANUP_QUERY_STRINGS', false );
defined( 'RX_REMOVE_JQUERY_MIGRATE' ) || define( 'RX_REMOVE_JQUERY_MIGRATE', false );
defined( 'RX_DISABLE_DASHICONS_GUESTS' ) || define( 'RX_DISABLE_DASHICONS_GUESTS', true );
defined( 'RX_DISABLE_BLOCK_LIBRARY_CSS' ) || define( 'RX_DISABLE_BLOCK_LIBRARY_CSS', false );
defined( 'RX_DISABLE_GLOBAL_STYLES' ) || define( 'RX_DISABLE_GLOBAL_STYLES', false );
defined( 'RX_DISABLE_CLASSIC_THEME_STYLES' ) || define( 'RX_DISABLE_CLASSIC_THEME_STYLES', false );
defined( 'RX_LIMIT_POST_REVISIONS' ) || define( 'RX_LIMIT_POST_REVISIONS', true );
defined( 'RX_DISABLE_HEARTBEAT_FRONTEND' ) || define( 'RX_DISABLE_HEARTBEAT_FRONTEND', false );
defined( 'RX_REMOVE_CAPITAL_P_DANGLING' ) || define( 'RX_REMOVE_CAPITAL_P_DANGLING', true );
/**
* ------------------------------------------------------------
* Main Cleanup Bootstrap
* ------------------------------------------------------------
*/
add_action( 'after_setup_theme', 'rx_theme_cleanup_after_setup', 20 );
function rx_theme_cleanup_after_setup() {
if ( RX_DISABLE_GENERATOR_TAG ) {
remove_action( 'wp_head', 'wp_generator' );
add_filter( 'the_generator', '__return_empty_string' );
}
if ( RX_DISABLE_RSD_LINK ) {
remove_action( 'wp_head', 'rsd_link' );
}
if ( RX_DISABLE_WLW_LINK ) {
remove_action( 'wp_head', 'wlwmanifest_link' );
}
if ( RX_DISABLE_SHORTLINK ) {
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'template_redirect', 'wp_shortlink_header', 11 );
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
if ( RX_DISABLE_REST_LINKS ) {
remove_action( 'wp_head', 'rest_output_link_wp_head' );
remove_action( 'template_redirect', 'rest_output_link_header', 11 );
remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
}
if ( RX_REMOVE_CAPITAL_P_DANGLING ) {
remove_filter( 'the_title', 'capital_P_dangit', 11 );
remove_filter( 'the_content', 'capital_P_dangit', 11 );
remove_filter( 'comment_text', 'capital_P_dangit', 31 );
}
/**
* Remove WordPress version from styles/scripts.
*/
add_filter( 'style_loader_src', 'rx_theme_remove_wp_version_from_assets', 9999 );
add_filter( 'script_loader_src', 'rx_theme_remove_wp_version_from_assets', 9999 );
}
/**
* ------------------------------------------------------------
* Head Cleanup
* ------------------------------------------------------------
*/
add_action( 'init', 'rx_theme_advanced_head_cleanup' );
function rx_theme_advanced_head_cleanup() {
/**
* Remove WordPress default meta links.
*/
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
/**
* Remove REST links only if enabled.
*/
if ( RX_DISABLE_REST_LINKS ) {
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 );
}
/**
* Remove extra feed links only if feeds are disabled.
*/
if ( RX_DISABLE_FEEDS ) {
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
}
}
/**
* Remove WordPress version query only from local assets.
*/
function rx_theme_remove_wp_version_from_assets( $src ) {
if ( empty( $src ) ) {
return $src;
}
$wp_version = get_bloginfo( 'version' );
if ( strpos( $src, 'ver=' . $wp_version ) !== false ) {
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
/**
* ------------------------------------------------------------
* Emoji Cleanup
* ------------------------------------------------------------
*/
if ( RX_DISABLE_EMOJIS ) {
add_action( 'init', 'rx_theme_disable_emojis' );
}
function rx_theme_disable_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_action( 'embed_head', 'print_emoji_detection_script' );
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( 'emoji_svg_url', '__return_false' );
add_filter( 'tiny_mce_plugins', 'rx_theme_disable_emojis_tinymce' );
add_filter( 'wp_resource_hints', 'rx_theme_disable_emoji_dns_prefetch', 10, 2 );
}
function rx_theme_disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
}
return array();
}
function rx_theme_disable_emoji_dns_prefetch( $urls, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/' );
$urls = array_filter(
$urls,
static function ( $url ) use ( $emoji_svg_url ) {
return false === strpos( $url, $emoji_svg_url );
}
);
}
return $urls;
}
/**
* ------------------------------------------------------------
* oEmbed Cleanup
* ------------------------------------------------------------
*/
if ( RX_DISABLE_EMBEDS ) {
add_action( 'init', 'rx_theme_disable_embeds', 9999 );
}
function rx_theme_disable_embeds() {
remove_action( 'rest_api_init', 'wp_oembed_register_route' );
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
add_filter( 'embed_oembed_discover', '__return_false' );
add_filter( 'tiny_mce_plugins', 'rx_theme_disable_embeds_tinymce' );
add_filter(
'rewrite_rules_array',
static function ( $rules ) {
foreach ( $rules as $rule => $rewrite ) {
if ( false !== strpos( $rewrite, 'embed=true' ) ) {
unset( $rules[ $rule ] );
}
}
return $rules;
}
);
add_action(
'wp_footer',
static function () {
wp_deregister_script( 'wp-embed' );
},
1
);
}
function rx_theme_disable_embeds_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpembed' ) );
}
return array();
}
/**
* ------------------------------------------------------------
* XML-RPC Cleanup
* ------------------------------------------------------------
*
* Warning:
* Disable XML-RPC only if you do not use Jetpack, remote publishing,
* mobile app publishing, or XML-RPC dependent services.
*/
if ( RX_DISABLE_XMLRPC ) {
add_filter( 'xmlrpc_enabled', '__return_false' );
add_filter( 'wp_headers', 'rx_theme_remove_x_pingback_header' );
add_filter( 'pings_open', '__return_false', 20, 2 );
add_filter( 'pre_update_option_enable_xmlrpc', '__return_false' );
add_filter( 'pre_option_enable_xmlrpc', '__return_zero' );
}
function rx_theme_remove_x_pingback_header( $headers ) {
if ( isset( $headers['X-Pingback'] ) ) {
unset( $headers['X-Pingback'] );
}
return $headers;
}
/**
* ------------------------------------------------------------
* REST API Cleanup
* ------------------------------------------------------------
*
* By default, this does not disable REST because Gutenberg,
* WooCommerce, Rank Math, Yoast, Elementor, and many plugins need it.
*/
add_filter( 'rest_endpoints', 'rx_theme_cleanup_rest_endpoints' );
function rx_theme_cleanup_rest_endpoints( $endpoints ) {
/**
* Remove user endpoint for non-admin users.
*/
if ( ! current_user_can( 'list_users' ) ) {
if ( isset( $endpoints['/wp/v2/users'] ) ) {
unset( $endpoints['/wp/v2/users'] );
}
if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
}
}
return $endpoints;
}
/**
* Remove REST user links from public post responses.
*/
add_filter( 'rest_prepare_post', 'rx_theme_remove_rest_author_links', 10, 3 );
add_filter( 'rest_prepare_page', 'rx_theme_remove_rest_author_links', 10, 3 );
function rx_theme_remove_rest_author_links( $response, $post, $request ) {
if ( ! current_user_can( 'list_users' ) && is_a( $response, 'WP_REST_Response' ) ) {
$links = $response->get_links();
if ( isset( $links['author'] ) ) {
$response->remove_link( 'author' );
}
}
return $response;
}
/**
* ------------------------------------------------------------
* Feed Cleanup
* ------------------------------------------------------------
*/
if ( RX_DISABLE_FEEDS ) {
add_action( 'do_feed', 'rx_theme_disable_feeds', 1 );
add_action( 'do_feed_rdf', 'rx_theme_disable_feeds', 1 );
add_action( 'do_feed_rss', 'rx_theme_disable_feeds', 1 );
add_action( 'do_feed_rss2', 'rx_theme_disable_feeds', 1 );
add_action( 'do_feed_atom', 'rx_theme_disable_feeds', 1 );
add_action( 'do_feed_rss2_comments', 'rx_theme_disable_feeds', 1 );
add_action( 'do_feed_atom_comments', 'rx_theme_disable_feeds', 1 );
}
function rx_theme_disable_feeds() {
wp_die(
esc_html__( 'Feeds are disabled on this website.', 'rx-theme' ),
esc_html__( 'Feeds Disabled', 'rx-theme' ),
array(
'response' => 410,
)
);
}
/**
* ------------------------------------------------------------
* Comments Cleanup
* ------------------------------------------------------------
*/
if ( RX_DISABLE_COMMENTS_FRONTEND ) {
add_action( 'admin_init', 'rx_theme_disable_comments_admin_redirect' );
add_action( 'admin_menu', 'rx_theme_disable_comments_admin_menu' );
add_action( 'init', 'rx_theme_disable_comments_post_types' );
add_action( 'wp_before_admin_bar_render', 'rx_theme_disable_comments_admin_bar' );
add_filter( 'comments_open', '__return_false', 20, 2 );
add_filter( 'pings_open', '__return_false', 20, 2 );
add_filter( 'comments_array', '__return_empty_array', 10, 2 );
}
function rx_theme_disable_comments_post_types() {
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
if ( post_type_supports( $post_type, 'comments' ) ) {
remove_post_type_support( $post_type, 'comments' );
}
if ( post_type_supports( $post_type, 'trackbacks' ) ) {
remove_post_type_support( $post_type, 'trackbacks' );
}
}
}
function rx_theme_disable_comments_admin_menu() {
remove_menu_page( 'edit-comments.php' );
}
function rx_theme_disable_comments_admin_redirect() {
global $pagenow;
if ( 'edit-comments.php' === $pagenow ) {
wp_safe_redirect( admin_url() );
exit;
}
}
function rx_theme_disable_comments_admin_bar() {
global $wp_admin_bar;
if ( is_object( $wp_admin_bar ) ) {
$wp_admin_bar->remove_menu( 'comments' );
}
}
/**
* ------------------------------------------------------------
* Attachment Page Cleanup
* ------------------------------------------------------------
*/
if ( RX_DISABLE_ATTACHMENT_PAGES ) {
add_action( 'template_redirect', 'rx_theme_redirect_attachment_pages', 1 );
}
function rx_theme_redirect_attachment_pages() {
if ( ! is_attachment() ) {
return;
}
global $post;
if ( $post && ! empty( $post->post_parent ) ) {
wp_safe_redirect( get_permalink( $post->post_parent ), 301 );
exit;
}
wp_safe_redirect( home_url( '/' ), 301 );
exit;
}
/**
* ------------------------------------------------------------
* Author Archive Cleanup
* ------------------------------------------------------------
*/
if ( RX_DISABLE_AUTHOR_ARCHIVES ) {
add_action( 'template_redirect', 'rx_theme_disable_author_archives' );
}
function rx_theme_disable_author_archives() {
if ( is_author() ) {
wp_safe_redirect( home_url( '/' ), 301 );
exit;
}
}
/**
* ------------------------------------------------------------
* Query String Cleanup
* ------------------------------------------------------------
*
* Keep disabled by default because many cache systems already handle this.
*/
if ( RX_CLEANUP_QUERY_STRINGS ) {
add_filter( 'script_loader_src', 'rx_theme_remove_asset_query_strings', 15 );
add_filter( 'style_loader_src', 'rx_theme_remove_asset_query_strings', 15 );
}
function rx_theme_remove_asset_query_strings( $src ) {
if ( is_admin() ) {
return $src;
}
if ( false !== strpos( $src, '?ver=' ) ) {
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
/**
* ------------------------------------------------------------
* jQuery Migrate Cleanup
* ------------------------------------------------------------
*
* Keep disabled by default. Some old plugins need jquery-migrate.
*/
if ( RX_REMOVE_JQUERY_MIGRATE ) {
add_action( 'wp_default_scripts', 'rx_theme_remove_jquery_migrate' );
}
function rx_theme_remove_jquery_migrate( $scripts ) {
if ( is_admin() ) {
return;
}
if ( isset( $scripts->registered['jquery'] ) ) {
$script = $scripts->registered['jquery'];
if ( ! empty( $script->deps ) ) {
$script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) );
}
}
}
/**
* ------------------------------------------------------------
* Dashicons Cleanup for Guests
* ------------------------------------------------------------
*/
if ( RX_DISABLE_DASHICONS_GUESTS ) {
add_action( 'wp_enqueue_scripts', 'rx_theme_disable_dashicons_for_guests', 100 );
}
function rx_theme_disable_dashicons_for_guests() {
if ( ! is_user_logged_in() ) {
wp_dequeue_style( 'dashicons' );
wp_deregister_style( 'dashicons' );
}
}
/**
* ------------------------------------------------------------
* Gutenberg / Block CSS Cleanup
* ------------------------------------------------------------
*/
add_action( 'wp_enqueue_scripts', 'rx_theme_cleanup_block_assets', 100 );
function rx_theme_cleanup_block_assets() {
if ( RX_DISABLE_BLOCK_LIBRARY_CSS ) {
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
}
if ( RX_DISABLE_GLOBAL_STYLES ) {
wp_dequeue_style( 'global-styles' );
}
if ( RX_DISABLE_CLASSIC_THEME_STYLES ) {
wp_dequeue_style( 'classic-theme-styles' );
}
}
/**
* Remove inline global styles if enabled.
*/
if ( RX_DISABLE_GLOBAL_STYLES ) {
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
}
/**
* ------------------------------------------------------------
* Heartbeat API Cleanup
* ------------------------------------------------------------
*/
if ( RX_DISABLE_HEARTBEAT_FRONTEND ) {
add_action( 'init', 'rx_theme_disable_heartbeat_frontend', 1 );
}
function rx_theme_disable_heartbeat_frontend() {
if ( ! is_admin() ) {
wp_deregister_script( 'heartbeat' );
}
}
/**
* Control heartbeat frequency in admin instead of fully disabling.
*/
add_filter( 'heartbeat_settings', 'rx_theme_heartbeat_settings' );
function rx_theme_heartbeat_settings( $settings ) {
$settings['interval'] = 60;
return $settings;
}
/**
* ------------------------------------------------------------
* Login Cleanup
* ------------------------------------------------------------
*/
add_filter( 'login_errors', 'rx_theme_simplify_login_errors' );
function rx_theme_simplify_login_errors() {
return esc_html__( 'Invalid login details.', 'rx-theme' );
}
/**
* Remove WordPress logo from login page header URL.
*/
add_filter( 'login_headerurl', 'rx_theme_login_header_url' );
function rx_theme_login_header_url() {
return home_url( '/' );
}
/**
* Change login logo title.
*/
add_filter( 'login_headertext', 'rx_theme_login_header_text' );
function rx_theme_login_header_text() {
return get_bloginfo( 'name' );
}
/**
* ------------------------------------------------------------
* Security Header Helpers
* ------------------------------------------------------------
*
* These are safe basic headers.
* CSP is not included because it can break external scripts, ads, analytics,
* fonts, CDN files, and app chunks if not configured carefully.
*/
add_action( 'send_headers', 'rx_theme_basic_security_headers' );
function rx_theme_basic_security_headers() {
if ( is_admin() ) {
return;
}
header( 'X-Content-Type-Options: nosniff' );
header( 'X-Frame-Options: SAMEORIGIN' );
header( 'Referrer-Policy: strict-origin-when-cross-origin' );
header( 'X-XSS-Protection: 0' );
if ( is_ssl() ) {
header( 'Strict-Transport-Security: max-age=31536000; includeSubDomains' );
}
}
/**
* ------------------------------------------------------------
* Disable File Editing from Dashboard
* ------------------------------------------------------------
*
* This is best placed in wp-config.php:
* define( 'DISALLOW_FILE_EDIT', true );
*
* This fallback defines it if not already defined.
*/
if ( ! defined( 'DISALLOW_FILE_EDIT' ) ) {
define( 'DISALLOW_FILE_EDIT', true );
}
/**
* ------------------------------------------------------------
* Revision Cleanup
* ------------------------------------------------------------
*
* Best controlled in wp-config.php.
* This theme-level fallback only applies if WP_POST_REVISIONS is not defined.
*/
if ( RX_LIMIT_POST_REVISIONS && ! defined( 'WP_POST_REVISIONS' ) ) {
define( 'WP_POST_REVISIONS', 5 );
}
/**
* ------------------------------------------------------------
* Excerpt Cleanup
* ------------------------------------------------------------
*/
add_filter( 'excerpt_more', 'rx_theme_excerpt_more' );
function rx_theme_excerpt_more() {
return '…';
}
add_filter( 'excerpt_length', 'rx_theme_excerpt_length', 20 );
function rx_theme_excerpt_length( $length ) {
return absint(
apply_filters( 'rx_theme_excerpt_length', 28 )
);
}
/**
* ------------------------------------------------------------
* Search Cleanup
* ------------------------------------------------------------
*/
/**
* Redirect empty search queries to home.
*/
add_action( 'template_redirect', 'rx_theme_redirect_empty_search' );
function rx_theme_redirect_empty_search() {
if ( is_search() && ! is_admin() ) {
$search_query = get_search_query();
if ( '' === trim( $search_query ) ) {
wp_safe_redirect( home_url( '/' ), 302 );
exit;
}
}
}
/**
* Exclude pages from search by default.
*/
add_action( 'pre_get_posts', 'rx_theme_search_only_posts' );
function rx_theme_search_only_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search() ) {
$query->set(
'post_type',
apply_filters(
'rx_theme_search_post_types',
array( 'post' )
)
);
}
}
/**
* ------------------------------------------------------------
* Admin Bar Cleanup
* ------------------------------------------------------------
*/
add_action( 'admin_bar_menu', 'rx_theme_cleanup_admin_bar', 999 );
function rx_theme_cleanup_admin_bar( $wp_admin_bar ) {
if ( ! is_object( $wp_admin_bar ) ) {
return;
}
$wp_admin_bar->remove_node( 'wp-logo' );
$wp_admin_bar->remove_node( 'about' );
$wp_admin_bar->remove_node( 'wporg' );
$wp_admin_bar->remove_node( 'documentation' );
$wp_admin_bar->remove_node( 'support-forums' );
$wp_admin_bar->remove_node( 'feedback' );
}
/**
* ------------------------------------------------------------
* Dashboard Widgets Cleanup
* ------------------------------------------------------------
*/
add_action( 'wp_dashboard_setup', 'rx_theme_cleanup_dashboard_widgets', 999 );
function rx_theme_cleanup_dashboard_widgets() {
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
if ( ! current_user_can( 'manage_options' ) ) {
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
}
}
/**
* ------------------------------------------------------------
* Admin Footer Cleanup
* ------------------------------------------------------------
*/
add_filter( 'admin_footer_text', 'rx_theme_admin_footer_text' );
function rx_theme_admin_footer_text() {
return sprintf(
esc_html__( 'Built with %s.', 'rx-theme' ),
'<strong>RX Theme</strong>'
);
}
add_filter( 'update_footer', 'rx_theme_admin_update_footer', 20 );
function rx_theme_admin_update_footer() {
return 'RX Theme ' . esc_html( RX_THEME_CLEANUP_VERSION );
}
/**
* ------------------------------------------------------------
* Admin Notice Cleanup for Non-Admins
* ------------------------------------------------------------
*/
add_action( 'admin_head', 'rx_theme_hide_admin_notices_for_non_admins' );
function rx_theme_hide_admin_notices_for_non_admins() {
if ( current_user_can( 'manage_options' ) ) {
return;
}
echo '<style>
.update-nag,
.notice,
.notice-error,
.notice-warning,
.notice-info,
.notice-success,
.updated,
.error {
display: none !important;
}
</style>';
}
/**
* ------------------------------------------------------------
* WordPress Update Notice Cleanup for Non-Admins
* ------------------------------------------------------------
*/
add_action( 'admin_init', 'rx_theme_hide_update_notices_for_non_admins' );
function rx_theme_hide_update_notices_for_non_admins() {
if ( current_user_can( 'update_core' ) ) {
return;
}
remove_action( 'admin_notices', 'update_nag', 3 );
}
/**
* ------------------------------------------------------------
* Disable Self Pingbacks
* ------------------------------------------------------------
*/
add_action( 'pre_ping', 'rx_theme_disable_self_pingbacks' );
function rx_theme_disable_self_pingbacks( &$links ) {
$home = home_url();
foreach ( $links as $key => $link ) {
if ( 0 === strpos( $link, $home ) ) {
unset( $links[ $key ] );
}
}
}
/**
* ------------------------------------------------------------
* Body Class Cleanup
* ------------------------------------------------------------
*/
add_filter( 'body_class', 'rx_theme_clean_body_classes' );
function rx_theme_clean_body_classes( $classes ) {
$remove = array(
'wp-custom-logo',
);
$classes = array_diff( $classes, $remove );
if ( is_singular() ) {
$classes[] = 'rx-singular';
}
if ( is_archive() ) {
$classes[] = 'rx-archive';
}
if ( is_search() ) {
$classes[] = 'rx-search';
}
if ( is_front_page() ) {
$classes[] = 'rx-front-page';
}
return array_unique( array_map( 'sanitize_html_class', $classes ) );
}
/**
* ------------------------------------------------------------
* Nav Menu Class Cleanup
* ------------------------------------------------------------
*/
add_filter( 'nav_menu_css_class', 'rx_theme_clean_nav_menu_classes', 20, 4 );
function rx_theme_clean_nav_menu_classes( $classes, $item, $args, $depth ) {
$allowed = array(
'menu-item',
'current-menu-item',
'current-menu-parent',
'current-menu-ancestor',
'menu-item-has-children',
);
$classes = array_intersect( $classes, $allowed );
$classes[] = 'rx-menu-item';
if ( 0 === (int) $depth ) {
$classes[] = 'rx-menu-item-top';
}
return array_unique( array_map( 'sanitize_html_class', $classes ) );
}
/**
* ------------------------------------------------------------
* Archive Title Cleanup
* ------------------------------------------------------------
*/
add_filter( 'get_the_archive_title', 'rx_theme_clean_archive_title' );
function rx_theme_clean_archive_title( $title ) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = get_the_author();
} elseif ( is_tax() ) {
$title = single_term_title( '', false );
} elseif ( is_post_type_archive() ) {
$title = post_type_archive_title( '', false );
}
return $title;
}
/**
* ------------------------------------------------------------
* Image / Media Cleanup
* ------------------------------------------------------------
*/
/**
* Add missing image attributes.
*/
add_filter( 'wp_get_attachment_image_attributes', 'rx_theme_attachment_image_attributes', 10, 3 );
function rx_theme_attachment_image_attributes( $attr, $attachment, $size ) {
if ( empty( $attr['loading'] ) ) {
$attr['loading'] = 'lazy';
}
if ( empty( $attr['decoding'] ) ) {
$attr['decoding'] = 'async';
}
return $attr;
}
/**
* Disable image srcset only if specifically filtered.
*/
add_filter( 'wp_calculate_image_srcset', 'rx_theme_maybe_disable_srcset', 10, 5 );
function rx_theme_maybe_disable_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
if ( apply_filters( 'rx_theme_disable_srcset', false, $attachment_id ) ) {
return false;
}
return $sources;
}
/**
* ------------------------------------------------------------
* Upload MIME Cleanup
* ------------------------------------------------------------
*/
add_filter( 'upload_mimes', 'rx_theme_secure_upload_mimes' );
function rx_theme_secure_upload_mimes( $mimes ) {
/**
* SVG is not enabled by default because unsafe SVG files can contain scripts.
* You can enable SVG safely only after adding proper SVG sanitization.
*/
if ( apply_filters( 'rx_theme_allow_svg_uploads', false ) ) {
$mimes['svg'] = 'image/svg+xml';
}
return $mimes;
}
/**
* ------------------------------------------------------------
* Admin Columns Cleanup
* ------------------------------------------------------------
*/
add_filter( 'manage_posts_columns', 'rx_theme_cleanup_post_columns' );
add_filter( 'manage_pages_columns', 'rx_theme_cleanup_post_columns' );
function rx_theme_cleanup_post_columns( $columns ) {
if ( isset( $columns['comments'] ) && RX_DISABLE_COMMENTS_FRONTEND ) {
unset( $columns['comments'] );
}
return $columns;
}
/**
* ------------------------------------------------------------
* WP Resource Hints Cleanup
* ------------------------------------------------------------
*/
add_filter( 'wp_resource_hints', 'rx_theme_resource_hints_cleanup', 10, 2 );
function rx_theme_resource_hints_cleanup( $urls, $relation_type ) {
if ( 'dns-prefetch' !== $relation_type ) {
return $urls;
}
$remove_hosts = apply_filters(
'rx_theme_remove_dns_prefetch_hosts',
array(
'//s.w.org',
)
);
foreach ( $urls as $key => $url ) {
if ( in_array( $url, $remove_hosts, true ) ) {
unset( $urls[ $key ] );
}
}
return array_values( $urls );
}
/**
* ------------------------------------------------------------
* Canonical Cleanup Helper
* ------------------------------------------------------------
*
* Keep WordPress canonical redirects on by default.
* Disable only for special headless/static use cases.
*/
add_filter( 'redirect_canonical', 'rx_theme_maybe_disable_canonical_redirects', 10, 2 );
function rx_theme_maybe_disable_canonical_redirects( $redirect_url, $requested_url ) {
if ( apply_filters( 'rx_theme_disable_canonical_redirects', false ) ) {
return false;
}
return $redirect_url;
}
/**
* ------------------------------------------------------------
* Admin Email Verification Cleanup
* ------------------------------------------------------------
*
* By default disabled because many site owners dislike frequent confirmation.
*/
add_filter( 'admin_email_check_interval', 'rx_theme_admin_email_check_interval' );
function rx_theme_admin_email_check_interval( $interval ) {
if ( apply_filters( 'rx_theme_disable_admin_email_check', false ) ) {
return 0;
}
return $interval;
}
/**
* ------------------------------------------------------------
* Password Protected Post Cleanup
* ------------------------------------------------------------
*/
add_filter( 'protected_title_format', 'rx_theme_protected_title_format' );
add_filter( 'private_title_format', 'rx_theme_private_title_format' );
function rx_theme_protected_title_format() {
return '%s';
}
function rx_theme_private_title_format() {
return '%s';
}
/**
* ------------------------------------------------------------
* Dangerous Request Cleanup
* ------------------------------------------------------------
*
* Basic protection against suspicious public query strings.
*/
add_action( 'init', 'rx_theme_block_suspicious_requests', 1 );
function rx_theme_block_suspicious_requests() {
if ( is_admin() || wp_doing_ajax() || wp_doing_cron() ) {
return;
}
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
if ( empty( $request_uri ) ) {
return;
}
$blocked_patterns = apply_filters(
'rx_theme_blocked_request_patterns',
array(
'base64_encode',
'base64_decode',
'eval(',
'GLOBALS[',
'_REQUEST',
'_SERVER',
'_POST',
'_GET',
'wp-config.php',
'.env',
'../',
'%2e%2e',
'<script',
)
);
foreach ( $blocked_patterns as $pattern ) {
if ( false !== stripos( $request_uri, $pattern ) ) {
status_header( 403 );
exit;
}
}
}
/**
* ------------------------------------------------------------
* Disable Application Passwords Optional
* ------------------------------------------------------------
*/
add_filter( 'wp_is_application_passwords_available', 'rx_theme_maybe_disable_application_passwords' );
function rx_theme_maybe_disable_application_passwords( $available ) {
if ( apply_filters( 'rx_theme_disable_application_passwords', false ) ) {
return false;
}
return $available;
}
/**
* ------------------------------------------------------------
* Disable User Enumeration by ?author=1
* ------------------------------------------------------------
*/
add_action( 'template_redirect', 'rx_theme_block_author_query_enumeration' );
function rx_theme_block_author_query_enumeration() {
if ( is_admin() ) {
return;
}
if ( isset( $_GET['author'] ) && ! is_user_logged_in() ) {
wp_safe_redirect( home_url( '/' ), 301 );
exit;
}
}
/**
* ------------------------------------------------------------
* Disable REST User Enumeration Query
* ------------------------------------------------------------
*/
add_filter( 'rest_user_query', 'rx_theme_block_rest_user_enumeration', 10, 2 );
function rx_theme_block_rest_user_enumeration( $prepared_args, $request ) {
if ( ! current_user_can( 'list_users' ) ) {
$prepared_args['include'] = array( 0 );
}
return $prepared_args;
}
/**
* ------------------------------------------------------------
* Clean wp_head Priority Debug Helper
* ------------------------------------------------------------
*
* Use this only when debugging:
* add_filter( 'rx_theme_debug_wp_head_hooks', '__return_true' );
*/
add_action( 'wp_head', 'rx_theme_debug_wp_head_hooks', 9999 );
function rx_theme_debug_wp_head_hooks() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( ! apply_filters( 'rx_theme_debug_wp_head_hooks', false ) ) {
return;
}
global $wp_filter;
if ( empty( $wp_filter['wp_head'] ) ) {
return;
}
echo "\n<!-- RX Theme wp_head hooks debug enabled -->\n";
}
/**
* ------------------------------------------------------------
* Disable WordPress Default Sitemaps Optional
* ------------------------------------------------------------
*
* Keep enabled by default.
* Disable if you use Rank Math, Yoast, custom sitemap.php, or another sitemap.
*/
add_filter( 'wp_sitemaps_enabled', 'rx_theme_maybe_disable_wp_sitemaps' );
function rx_theme_maybe_disable_wp_sitemaps( $enabled ) {
if ( apply_filters( 'rx_theme_disable_wp_sitemaps', false ) ) {
return false;
}
return $enabled;
}
/**
* ------------------------------------------------------------
* Disable Big Image Size Threshold Optional
* ------------------------------------------------------------
*/
add_filter( 'big_image_size_threshold', 'rx_theme_big_image_size_threshold' );
function rx_theme_big_image_size_threshold( $threshold ) {
$new_threshold = apply_filters( 'rx_theme_big_image_threshold', $threshold );
if ( false === $new_threshold ) {
return false;
}
return absint( $new_threshold );
}
/**
* ------------------------------------------------------------
* Clean Category / Tag Description Output
* ------------------------------------------------------------
*/
remove_filter( 'pre_term_description', 'wp_filter_kses' );
remove_filter( 'term_description', 'wp_kses_data' );
add_filter( 'term_description', 'rx_theme_clean_term_description', 20 );
function rx_theme_clean_term_description( $description ) {
return wp_kses_post( $description );
}
/**
* ------------------------------------------------------------
* Remove Recent Comments Inline CSS
* ------------------------------------------------------------
*/
add_action( 'widgets_init', 'rx_theme_remove_recent_comments_style' );
function rx_theme_remove_recent_comments_style() {
global $wp_widget_factory;
if (
isset( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'] )
&& is_object( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'] )
) {
remove_action(
'wp_head',
array(
$wp_widget_factory->widgets['WP_Widget_Recent_Comments'],
'recent_comments_style',
)
);
}
}
/**
* ------------------------------------------------------------
* Remove Default Gallery Inline CSS
* ------------------------------------------------------------
*/
add_filter( 'use_default_gallery_style', '__return_false' );
/**
* ------------------------------------------------------------
* Disable Admin Color Scheme Picker Optional
* ------------------------------------------------------------
*/
add_action( 'admin_head-profile.php', 'rx_theme_maybe_hide_admin_color_picker' );
add_action( 'admin_head-user-edit.php', 'rx_theme_maybe_hide_admin_color_picker' );
function rx_theme_maybe_hide_admin_color_picker() {
if ( ! apply_filters( 'rx_theme_hide_admin_color_picker', false ) ) {
return;
}
echo '<style>.user-admin-color-wrap{display:none!important;}</style>';
}
/**
* ------------------------------------------------------------
* Clean User Contact Methods
* ------------------------------------------------------------
*/
add_filter( 'user_contactmethods', 'rx_theme_cleanup_user_contact_methods' );
function rx_theme_cleanup_user_contact_methods( $methods ) {
unset( $methods['aim'], $methods['yim'], $methods['jabber'] );
$methods['facebook'] = esc_html__( 'Facebook URL', 'rx-theme' );
$methods['twitter'] = esc_html__( 'Twitter/X URL', 'rx-theme' );
$methods['linkedin'] = esc_html__( 'LinkedIn URL', 'rx-theme' );
$methods['instagram'] = esc_html__( 'Instagram URL', 'rx-theme' );
return $methods;
}
/**
* ------------------------------------------------------------
* Disable Admin File Modifications Optional
* ------------------------------------------------------------
*
* Best added in wp-config.php:
* define( 'DISALLOW_FILE_MODS', true );
*/
if ( apply_filters( 'rx_theme_disallow_file_mods', false ) && ! defined( 'DISALLOW_FILE_MODS' ) ) {
define( 'DISALLOW_FILE_MODS', true );
}
/**
* ------------------------------------------------------------
* Clean Default Widgets Optional
* ------------------------------------------------------------
*/
add_action( 'widgets_init', 'rx_theme_unregister_default_widgets', 20 );
function rx_theme_unregister_default_widgets() {
if ( ! apply_filters( 'rx_theme_unregister_default_widgets', false ) ) {
return;
}
unregister_widget( 'WP_Widget_Pages' );
unregister_widget( 'WP_Widget_Calendar' );
unregister_widget( 'WP_Widget_Archives' );
unregister_widget( 'WP_Widget_Links' );
unregister_widget( 'WP_Widget_Meta' );
unregister_widget( 'WP_Widget_Search' );
unregister_widget( 'WP_Widget_Text' );
unregister_widget( 'WP_Widget_Categories' );
unregister_widget( 'WP_Widget_Recent_Posts' );
unregister_widget( 'WP_Widget_Recent_Comments' );
unregister_widget( 'WP_Widget_RSS' );
unregister_widget( 'WP_Widget_Tag_Cloud' );
unregister_widget( 'WP_Nav_Menu_Widget' );
}
/**
* ------------------------------------------------------------
* Clean Post Classes
* ------------------------------------------------------------
*/
add_filter( 'post_class', 'rx_theme_clean_post_classes', 20, 3 );
function rx_theme_clean_post_classes( $classes, $class, $post_id ) {
$classes[] = 'rx-post';
if ( is_sticky( $post_id ) ) {
$classes[] = 'rx-post-sticky';
}
if ( has_post_thumbnail( $post_id ) ) {
$classes[] = 'rx-post-has-thumbnail';
} else {
$classes[] = 'rx-post-no-thumbnail';
}
return array_unique( array_map( 'sanitize_html_class', $classes ) );
}
/**
* ------------------------------------------------------------
* Clean Comment Form Defaults
* ------------------------------------------------------------
*/
add_filter( 'comment_form_default_fields', 'rx_theme_comment_form_default_fields' );
function rx_theme_comment_form_default_fields( $fields ) {
if ( isset( $fields['url'] ) && apply_filters( 'rx_theme_remove_comment_url_field', true ) ) {
unset( $fields['url'] );
}
return $fields;
}
/**
* ------------------------------------------------------------
* Remove Website Field from Comment Author Link
* ------------------------------------------------------------
*/
add_filter( 'get_comment_author_link', 'rx_theme_comment_author_link_cleanup' );
function rx_theme_comment_author_link_cleanup( $return ) {
if ( apply_filters( 'rx_theme_disable_comment_author_links', true ) ) {
return wp_strip_all_tags( $return );
}
return $return;
}
/**
* ------------------------------------------------------------
* Disable HTML in Comments Optional
* ------------------------------------------------------------
*/
add_filter( 'preprocess_comment', 'rx_theme_maybe_disable_comment_html' );
function rx_theme_maybe_disable_comment_html( $commentdata ) {
if ( apply_filters( 'rx_theme_disable_comment_html', false ) && isset( $commentdata['comment_content'] ) ) {
$commentdata['comment_content'] = wp_strip_all_tags( $commentdata['comment_content'] );
}
return $commentdata;
}
/**
* ------------------------------------------------------------
* Remove Comment Reply Script When Not Needed
* ------------------------------------------------------------
*/
add_action( 'wp_enqueue_scripts', 'rx_theme_comment_reply_cleanup', 100 );
function rx_theme_comment_reply_cleanup() {
if ( ! is_singular() || ! comments_open() || ! get_option( 'thread_comments' ) ) {
wp_dequeue_script( 'comment-reply' );
}
}
/**
* ------------------------------------------------------------
* Disable Lazy Loading on First Featured Image Optional
* ------------------------------------------------------------
*/
add_filter( 'wp_get_attachment_image_attributes', 'rx_theme_featured_image_loading_priority', 20, 3 );
function rx_theme_featured_image_loading_priority( $attr, $attachment, $size ) {
if ( ! is_singular() || ! in_the_loop() || ! is_main_query() ) {
return $attr;
}
if ( apply_filters( 'rx_theme_prioritize_singular_featured_image', true ) ) {
$attr['fetchpriority'] = 'high';
if ( isset( $attr['loading'] ) ) {
unset( $attr['loading'] );
}
}
return $attr;
}
/**
* ------------------------------------------------------------
* Clean WordPress Robots Meta Optional
* ------------------------------------------------------------
*/
add_filter( 'wp_robots', 'rx_theme_wp_robots_cleanup' );
function rx_theme_wp_robots_cleanup( $robots ) {
if ( is_search() && apply_filters( 'rx_theme_noindex_search_results', true ) ) {
$robots['noindex'] = true;
$robots['follow'] = true;
}
if ( is_404() && apply_filters( 'rx_theme_noindex_404', true ) ) {
$robots['noindex'] = true;
$robots['follow'] = true;
}
return $robots;
}
/**
* ------------------------------------------------------------
* Disable Admin Bar Frontend for Non-Admins Optional
* ------------------------------------------------------------
*/
add_filter( 'show_admin_bar', 'rx_theme_maybe_hide_admin_bar' );
function rx_theme_maybe_hide_admin_bar( $show ) {
if ( apply_filters( 'rx_theme_hide_admin_bar_for_non_admins', false ) && ! current_user_can( 'manage_options' ) ) {
return false;
}
return $show;
}
/**
* ------------------------------------------------------------
* Clean Extra Rel Links
* ------------------------------------------------------------
*/
add_filter( 'the_content', 'rx_theme_clean_empty_paragraphs', 20 );
function rx_theme_clean_empty_paragraphs( $content ) {
if ( is_admin() || empty( $content ) ) {
return $content;
}
if ( ! apply_filters( 'rx_theme_clean_empty_paragraphs', false ) ) {
return $content;
}
$content = preg_replace( '#<p>\s*</p>#', '', $content );
return $content;
}
/**
* ------------------------------------------------------------
* Safe Output Buffer Cleanup Optional
* ------------------------------------------------------------
*
* Disabled by default. Use only if your HTML has many whitespace problems.
*/
add_action( 'template_redirect', 'rx_theme_maybe_start_html_cleanup_buffer' );
function rx_theme_maybe_start_html_cleanup_buffer() {
if (
is_admin()
|| wp_doing_ajax()
|| wp_doing_cron()
|| ! apply_filters( 'rx_theme_enable_html_buffer_cleanup', false )
) {
return;
}
ob_start( 'rx_theme_html_buffer_cleanup' );
}
function rx_theme_html_buffer_cleanup( $html ) {
if ( empty( $html ) ) {
return $html;
}
$html = preg_replace( '/\s+/', ' ', $html );
$html = str_replace( array( '> <' ), array( '><' ), $html );
return $html;
}
/**
* ------------------------------------------------------------
* Plugin Compatibility: WooCommerce Safe Cleanup
* ------------------------------------------------------------
*/
add_action( 'wp_enqueue_scripts', 'rx_theme_woocommerce_safe_cleanup', 99 );
function rx_theme_woocommerce_safe_cleanup() {
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
/**
* Do not remove WooCommerce assets on shop/cart/checkout/account pages.
*/
if (
function_exists( 'is_woocommerce' )
&& (
is_woocommerce()
|| is_cart()
|| is_checkout()
|| is_account_page()
)
) {
return;
}
if ( apply_filters( 'rx_theme_dequeue_woocommerce_assets_outside_shop', false ) ) {
wp_dequeue_style( 'woocommerce-general' );
wp_dequeue_style( 'woocommerce-layout' );
wp_dequeue_style( 'woocommerce-smallscreen' );
wp_dequeue_script( 'wc-cart-fragments' );
}
}
/**
* ------------------------------------------------------------
* Final Developer Hooks
* ------------------------------------------------------------
*/
do_action( 'rx_theme_cleanup_loaded' );
Important safe settings
This file includes many features, but the risky ones are disabled by default. For example:
defined( 'RX_DISABLE_FEEDS' ) || define( 'RX_DISABLE_FEEDS', false );
defined( 'RX_DISABLE_REST_LINKS' ) || define( 'RX_DISABLE_REST_LINKS', false );
defined( 'RX_DISABLE_COMMENTS_FRONTEND' ) || define( 'RX_DISABLE_COMMENTS_FRONTEND', false );
defined( 'RX_DISABLE_BLOCK_LIBRARY_CSS' ) || define( 'RX_DISABLE_BLOCK_LIBRARY_CSS', false );
You can turn them on later from wp-config.php or by editing the constants.
For your RX Theme, this is a strong cleanup file because it handles:
Head cleanup, emoji removal, oEmbed cleanup, XML-RPC control, REST user protection, attachment redirect, optional comments removal, Dashicons cleanup, security headers, admin cleanup, dashboard cleanup, search cleanup, image lazy loading, comment form cleanup, author enumeration blocking, WooCommerce-safe cleanup, and developer filters for future expansion.