I’ll build this as a production-style performance/resource-hints.php module for your rx-theme, focused on DNS-prefetch, preconnect, preload, prefetch, pre-render safety, font hints, image hints, and admin-customizable filters.
Below is an advanced performance/resource-hints.php file for your rx-theme.
Create this file:
rx-theme/inc/performance/resource-hints.php
Then include it from functions.php:
require_once get_template_directory() . '/inc/performance/resource-hints.php';
performance/resource-hints.php
<?php
/**
* RX Theme Resource Hints & Performance Optimizer
*
* File: inc/performance/resource-hints.php
*
* Features:
* - DNS prefetch
* - Preconnect
* - Preload fonts
* - Preload CSS
* - Preload JavaScript
* - Prefetch important pages
* - Prerender optional pages
* - Modulepreload support
* - Early image preload
* - WooCommerce-aware hints
* - Google Fonts optimization
* - CDN hints
* - REST API hints
* - Headless/frontend app asset hints
* - Safe filter system for future extension
*
* @package RX_Theme
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'RX_Theme_Resource_Hints' ) ) :
final class RX_Theme_Resource_Hints {
/**
* Class version.
*/
const VERSION = '1.0.0';
/**
* Whether hints are enabled.
*
* @var bool
*/
private static bool $enabled = true;
/**
* Cached site host.
*
* @var string|null
*/
private static ?string $site_host = null;
/**
* Boot the module.
*/
public static function init(): void {
self::$enabled = (bool) apply_filters( 'rx_theme_resource_hints_enabled', true );
if ( ! self::$enabled ) {
return;
}
add_filter( 'wp_resource_hints', array( __CLASS__, 'wp_resource_hints' ), 10, 2 );
add_action( 'wp_head', array( __CLASS__, 'print_preconnect_hints' ), 0 );
add_action( 'wp_head', array( __CLASS__, 'print_dns_prefetch_hints' ), 1 );
add_action( 'wp_head', array( __CLASS__, 'print_preload_hints' ), 2 );
add_action( 'wp_head', array( __CLASS__, 'print_modulepreload_hints' ), 3 );
add_action( 'wp_head', array( __CLASS__, 'print_prefetch_hints' ), 4 );
add_action( 'wp_head', array( __CLASS__, 'print_prerender_hints' ), 5 );
add_filter( 'style_loader_tag', array( __CLASS__, 'maybe_optimize_stylesheet_tag' ), 10, 4 );
add_filter( 'script_loader_tag', array( __CLASS__, 'maybe_optimize_script_tag' ), 10, 3 );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_optional_asset_data' ), 99 );
}
/**
* Extend WordPress native resource hints.
*
* @param array $urls URLs.
* @param string $relation_type Relation type.
* @return array
*/
public static function wp_resource_hints( array $urls, string $relation_type ): array {
if ( is_admin() || self::is_login_page() ) {
return $urls;
}
$extra = array();
if ( 'dns-prefetch' === $relation_type ) {
$extra = self::get_dns_prefetch_urls();
}
if ( 'preconnect' === $relation_type ) {
$extra = self::get_preconnect_urls();
}
$urls = array_merge( $urls, $extra );
return self::clean_unique_urls( $urls );
}
/**
* Print preconnect hints manually.
*/
public static function print_preconnect_hints(): void {
if ( ! self::should_print_frontend_hints() ) {
return;
}
$urls = self::get_preconnect_urls();
foreach ( $urls as $url ) {
$crossorigin = self::needs_crossorigin( $url ) ? ' crossorigin' : '';
printf(
'<link rel="preconnect" href="%s"%s>' . "\n",
esc_url( $url ),
$crossorigin
);
}
}
/**
* Print DNS prefetch hints manually.
*/
public static function print_dns_prefetch_hints(): void {
if ( ! self::should_print_frontend_hints() ) {
return;
}
$urls = self::get_dns_prefetch_urls();
foreach ( $urls as $url ) {
printf(
'<link rel="dns-prefetch" href="%s">' . "\n",
esc_url( $url )
);
}
}
/**
* Print preload hints.
*/
public static function print_preload_hints(): void {
if ( ! self::should_print_frontend_hints() ) {
return;
}
$preloads = self::get_preload_items();
foreach ( $preloads as $item ) {
self::print_preload_item( $item );
}
}
/**
* Print modulepreload hints.
*/
public static function print_modulepreload_hints(): void {
if ( ! self::should_print_frontend_hints() ) {
return;
}
$items = self::get_modulepreload_items();
foreach ( $items as $item ) {
if ( empty( $item['href'] ) ) {
continue;
}
$attrs = array(
'rel' => 'modulepreload',
'href' => esc_url( $item['href'] ),
);
if ( ! empty( $item['crossorigin'] ) ) {
$attrs['crossorigin'] = esc_attr( $item['crossorigin'] );
}
echo '<link ' . self::build_attributes( $attrs ) . '>' . "\n";
}
}
/**
* Print prefetch hints.
*/
public static function print_prefetch_hints(): void {
if ( ! self::should_print_frontend_hints() ) {
return;
}
$items = self::get_prefetch_items();
foreach ( $items as $item ) {
if ( empty( $item['href'] ) ) {
continue;
}
$attrs = array(
'rel' => 'prefetch',
'href' => esc_url( $item['href'] ),
);
if ( ! empty( $item['as'] ) ) {
$attrs['as'] = esc_attr( $item['as'] );
}
if ( ! empty( $item['type'] ) ) {
$attrs['type'] = esc_attr( $item['type'] );
}
if ( ! empty( $item['crossorigin'] ) ) {
$attrs['crossorigin'] = esc_attr( $item['crossorigin'] );
}
echo '<link ' . self::build_attributes( $attrs ) . '>' . "\n";
}
}
/**
* Print prerender hints.
*/
public static function print_prerender_hints(): void {
if ( ! self::should_print_frontend_hints() ) {
return;
}
$enabled = (bool) apply_filters( 'rx_theme_prerender_enabled', false );
if ( ! $enabled ) {
return;
}
$items = self::get_prerender_items();
foreach ( $items as $href ) {
if ( empty( $href ) ) {
continue;
}
printf(
'<link rel="prerender" href="%s">' . "\n",
esc_url( $href )
);
}
}
/**
* Register optional inline data for future usage.
*/
public static function register_optional_asset_data(): void {
/**
* This method is intentionally kept for future expansion.
* You can use this hook to gather dynamic asset URLs from theme settings,
* manifest files, CDN config, or app build output.
*/
do_action( 'rx_theme_resource_hints_register_optional_asset_data' );
}
/**
* Get DNS prefetch URLs.
*
* @return array
*/
private static function get_dns_prefetch_urls(): array {
$urls = array(
'//fonts.googleapis.com',
'//fonts.gstatic.com',
'//www.google-analytics.com',
'//www.googletagmanager.com',
'//pagead2.googlesyndication.com',
'//googleads.g.doubleclick.net',
'//securepubads.g.doubleclick.net',
'//tpc.googlesyndication.com',
'//www.gstatic.com',
'//ajax.googleapis.com',
'//cdnjs.cloudflare.com',
'//cdn.jsdelivr.net',
'//unpkg.com',
'//polyfill.io',
'//static.cloudflareinsights.com',
'//platform.twitter.com',
'//connect.facebook.net',
'//graph.facebook.com',
'//www.youtube.com',
'//i.ytimg.com',
'//player.vimeo.com',
'//f.vimeocdn.com',
'//s.w.org',
);
$cdn_host = self::get_theme_cdn_host();
if ( $cdn_host ) {
$urls[] = '//' . $cdn_host;
}
$static_hosts = self::get_custom_static_hosts();
foreach ( $static_hosts as $host ) {
$urls[] = '//' . $host;
}
return apply_filters(
'rx_theme_dns_prefetch_urls',
self::clean_unique_urls( $urls )
);
}
/**
* Get preconnect URLs.
*
* @return array
*/
private static function get_preconnect_urls(): array {
$urls = array(
'https://fonts.googleapis.com',
'https://fonts.gstatic.com',
);
$cdn_host = self::get_theme_cdn_host();
if ( $cdn_host ) {
$urls[] = 'https://' . $cdn_host;
}
$static_hosts = self::get_custom_static_hosts();
foreach ( $static_hosts as $host ) {
$urls[] = 'https://' . $host;
}
if ( self::is_woocommerce_active() ) {
$urls[] = 'https://stats.wp.com';
$urls[] = 'https://pixel.wp.com';
}
return apply_filters(
'rx_theme_preconnect_urls',
self::clean_unique_urls( $urls )
);
}
/**
* Get preload items.
*
* @return array
*/
private static function get_preload_items(): array {
$items = array();
/*
* Theme main stylesheet preload.
*/
$main_css = get_stylesheet_uri();
if ( $main_css ) {
$items[] = array(
'href' => $main_css,
'as' => 'style',
'type' => 'text/css',
);
}
/*
* Optional critical CSS file.
*/
$critical_css_path = get_template_directory() . '/assets/css/critical.css';
$critical_css_url = get_template_directory_uri() . '/assets/css/critical.css';
if ( file_exists( $critical_css_path ) ) {
$items[] = array(
'href' => $critical_css_url,
'as' => 'style',
'type' => 'text/css',
);
}
/*
* Optional main app CSS.
*/
$app_css_path = get_template_directory() . '/assets/app/app.css';
$app_css_url = get_template_directory_uri() . '/assets/app/app.css';
if ( file_exists( $app_css_path ) ) {
$items[] = array(
'href' => $app_css_url,
'as' => 'style',
'type' => 'text/css',
);
}
/*
* Optional main app JavaScript.
*/
$app_js_path = get_template_directory() . '/assets/app/app.js';
$app_js_url = get_template_directory_uri() . '/assets/app/app.js';
if ( file_exists( $app_js_path ) ) {
$items[] = array(
'href' => $app_js_url,
'as' => 'script',
'type' => 'application/javascript',
);
}
/*
* Font preload examples.
* Add your real font files inside:
* assets/fonts/
*/
$font_files = array(
'Inter-Regular.woff2',
'Inter-Medium.woff2',
'Inter-SemiBold.woff2',
'Inter-Bold.woff2',
'Roboto-Regular.woff2',
'Roboto-Medium.woff2',
'Roboto-Bold.woff2',
);
foreach ( $font_files as $font_file ) {
$path = get_template_directory() . '/assets/fonts/' . $font_file;
$url = get_template_directory_uri() . '/assets/fonts/' . $font_file;
if ( file_exists( $path ) ) {
$items[] = array(
'href' => $url,
'as' => 'font',
'type' => 'font/woff2',
'crossorigin' => 'anonymous',
);
}
}
/*
* Hero image preload.
*/
$hero_image = self::get_hero_image_url();
if ( $hero_image ) {
$items[] = array(
'href' => $hero_image,
'as' => 'image',
'fetchpriority' => 'high',
);
}
/*
* WooCommerce single product image preload.
*/
if ( self::is_woocommerce_active() && function_exists( 'is_product' ) && is_product() ) {
$product_image = self::get_current_post_thumbnail_url();
if ( $product_image ) {
$items[] = array(
'href' => $product_image,
'as' => 'image',
'fetchpriority' => 'high',
);
}
}
/*
* Single post featured image preload.
*/
if ( is_single() ) {
$featured_image = self::get_current_post_thumbnail_url();
if ( $featured_image ) {
$items[] = array(
'href' => $featured_image,
'as' => 'image',
'fetchpriority' => 'high',
);
}
}
/*
* Custom CDN app assets.
*/
$cdn_assets = self::get_custom_cdn_preload_assets();
foreach ( $cdn_assets as $asset ) {
if ( ! empty( $asset['href'] ) && ! empty( $asset['as'] ) ) {
$items[] = $asset;
}
}
$items = apply_filters( 'rx_theme_preload_items', $items );
return self::unique_resource_items( $items );
}
/**
* Get modulepreload items.
*
* @return array
*/
private static function get_modulepreload_items(): array {
$items = array();
$module_files = array(
'assets/app/main.module.js',
'assets/app/router.module.js',
'assets/app/vendor.module.js',
);
foreach ( $module_files as $file ) {
$path = get_template_directory() . '/' . $file;
$url = get_template_directory_uri() . '/' . $file;
if ( file_exists( $path ) ) {
$items[] = array(
'href' => $url,
'crossorigin' => 'anonymous',
);
}
}
$items = apply_filters( 'rx_theme_modulepreload_items', $items );
return self::unique_resource_items( $items );
}
/**
* Get prefetch items.
*
* @return array
*/
private static function get_prefetch_items(): array {
$items = array();
/*
* Prefetch homepage when user is not already there.
*/
if ( ! is_front_page() ) {
$items[] = array(
'href' => home_url( '/' ),
'as' => 'document',
);
}
/*
* Prefetch blog page.
*/
$posts_page_id = (int) get_option( 'page_for_posts' );
if ( $posts_page_id ) {
$posts_url = get_permalink( $posts_page_id );
if ( $posts_url ) {
$items[] = array(
'href' => $posts_url,
'as' => 'document',
);
}
}
/*
* Prefetch next post on single post pages.
*/
if ( is_single() ) {
$next_post = get_next_post();
if ( $next_post instanceof WP_Post ) {
$next_url = get_permalink( $next_post );
if ( $next_url ) {
$items[] = array(
'href' => $next_url,
'as' => 'document',
);
}
}
}
/*
* Prefetch next paginated archive.
*/
if ( is_archive() || is_home() || is_search() ) {
$paged = max( 1, (int) get_query_var( 'paged' ) );
$next_url = get_pagenum_link( $paged + 1 );
if ( $next_url ) {
$items[] = array(
'href' => $next_url,
'as' => 'document',
);
}
}
/*
* Optional app route files.
*/
$route_files = array(
'assets/app/routes/home.js',
'assets/app/routes/post.js',
'assets/app/routes/archive.js',
'assets/app/routes/search.js',
);
foreach ( $route_files as $file ) {
$path = get_template_directory() . '/' . $file;
$url = get_template_directory_uri() . '/' . $file;
if ( file_exists( $path ) ) {
$items[] = array(
'href' => $url,
'as' => 'script',
'type' => 'application/javascript',
);
}
}
$items = apply_filters( 'rx_theme_prefetch_items', $items );
return self::unique_resource_items( $items );
}
/**
* Get prerender items.
*
* @return array
*/
private static function get_prerender_items(): array {
$items = array();
if ( ! is_front_page() ) {
$items[] = home_url( '/' );
}
$items = apply_filters( 'rx_theme_prerender_items', $items );
return self::clean_unique_urls( $items );
}
/**
* Print a preload item.
*
* @param array $item Preload item.
*/
private static function print_preload_item( array $item ): void {
if ( empty( $item['href'] ) || empty( $item['as'] ) ) {
return;
}
$attrs = array(
'rel' => 'preload',
'href' => esc_url( $item['href'] ),
'as' => esc_attr( $item['as'] ),
);
if ( ! empty( $item['type'] ) ) {
$attrs['type'] = esc_attr( $item['type'] );
}
if ( ! empty( $item['media'] ) ) {
$attrs['media'] = esc_attr( $item['media'] );
}
if ( ! empty( $item['imagesrcset'] ) ) {
$attrs['imagesrcset'] = esc_attr( $item['imagesrcset'] );
}
if ( ! empty( $item['imagesizes'] ) ) {
$attrs['imagesizes'] = esc_attr( $item['imagesizes'] );
}
if ( ! empty( $item['fetchpriority'] ) ) {
$attrs['fetchpriority'] = esc_attr( $item['fetchpriority'] );
}
if ( ! empty( $item['crossorigin'] ) ) {
$attrs['crossorigin'] = esc_attr( $item['crossorigin'] );
} elseif ( 'font' === $item['as'] ) {
$attrs['crossorigin'] = 'anonymous';
}
echo '<link ' . self::build_attributes( $attrs ) . '>' . "\n";
}
/**
* Optimize stylesheet tags.
*
* @param string $html HTML.
* @param string $handle Handle.
* @param string $href Href.
* @param string $media Media.
* @return string
*/
public static function maybe_optimize_stylesheet_tag( string $html, string $handle, string $href, string $media ): string {
$async_handles = apply_filters(
'rx_theme_async_style_handles',
array(
'rx-non-critical',
'rx-icons',
'rx-animation',
'rx-print',
)
);
if ( in_array( $handle, $async_handles, true ) ) {
$html = sprintf(
'<link rel="preload" href="%1$s" as="style" onload="this.onload=null;this.rel=\'stylesheet\'" media="%2$s">' . "\n" .
'<noscript><link rel="stylesheet" href="%1$s" media="%2$s"></noscript>' . "\n",
esc_url( $href ),
esc_attr( $media )
);
}
return $html;
}
/**
* Optimize script tags.
*
* @param string $tag Tag HTML.
* @param string $handle Script handle.
* @param string $src Script source.
* @return string
*/
public static function maybe_optimize_script_tag( string $tag, string $handle, string $src ): string {
if ( is_admin() || empty( $src ) ) {
return $tag;
}
$defer_handles = apply_filters(
'rx_theme_defer_script_handles',
array(
'rx-theme',
'rx-app',
'rx-main',
'rx-vendor',
'rx-runtime',
)
);
$async_handles = apply_filters(
'rx_theme_async_script_handles',
array(
'rx-analytics',
'rx-ads',
'rx-tracking',
)
);
$module_handles = apply_filters(
'rx_theme_module_script_handles',
array(
'rx-module',
'rx-app-module',
)
);
if ( in_array( $handle, $module_handles, true ) ) {
return sprintf(
'<script type="module" src="%s"></script>' . "\n",
esc_url( $src )
);
}
if ( in_array( $handle, $async_handles, true ) ) {
if ( false === strpos( $tag, ' async' ) ) {
$tag = str_replace( '<script ', '<script async ', $tag );
}
}
if ( in_array( $handle, $defer_handles, true ) ) {
if ( false === strpos( $tag, ' defer' ) ) {
$tag = str_replace( '<script ', '<script defer ', $tag );
}
}
return $tag;
}
/**
* Get theme CDN host.
*
* @return string
*/
private static function get_theme_cdn_host(): string {
$host = '';
/**
* Example:
* add_filter( 'rx_theme_cdn_host', fn() => 'static-assets-cdnrx.rxharun.com' );
*/
$host = (string) apply_filters( 'rx_theme_cdn_host', $host );
$host = preg_replace( '#^https?://#', '', $host );
$host = trim( $host, "/ \t\n\r\0\x0B" );
return sanitize_text_field( $host );
}
/**
* Get custom static hosts.
*
* @return array
*/
private static function get_custom_static_hosts(): array {
$hosts = apply_filters(
'rx_theme_static_hosts',
array(
'static-assets-cdnrx.rxharun.com',
)
);
if ( ! is_array( $hosts ) ) {
return array();
}
$clean = array();
foreach ( $hosts as $host ) {
$host = preg_replace( '#^https?://#', '', (string) $host );
$host = trim( $host, "/ \t\n\r\0\x0B" );
if ( $host ) {
$clean[] = sanitize_text_field( $host );
}
}
return array_values( array_unique( $clean ) );
}
/**
* Get custom CDN preload assets.
*
* @return array
*/
private static function get_custom_cdn_preload_assets(): array {
$assets = array(
/*
* Example:
*
* array(
* 'href' => 'https://static-assets-cdnrx.rxharun.com/_next/static/chunks/example.js',
* 'as' => 'script',
* 'type' => 'application/javascript',
* 'crossorigin' => 'anonymous',
* ),
*/
);
$assets = apply_filters( 'rx_theme_cdn_preload_assets', $assets );
return is_array( $assets ) ? $assets : array();
}
/**
* Get hero image URL.
*
* @return string
*/
private static function get_hero_image_url(): string {
$url = '';
if ( is_front_page() ) {
$custom_logo_id = (int) get_theme_mod( 'custom_logo' );
if ( $custom_logo_id ) {
$logo = wp_get_attachment_image_url( $custom_logo_id, 'full' );
if ( $logo ) {
$url = $logo;
}
}
}
$url = apply_filters( 'rx_theme_hero_image_preload_url', $url );
return esc_url_raw( (string) $url );
}
/**
* Get current post thumbnail URL.
*
* @return string
*/
private static function get_current_post_thumbnail_url(): string {
if ( ! is_singular() ) {
return '';
}
$post_id = get_queried_object_id();
if ( ! $post_id || ! has_post_thumbnail( $post_id ) ) {
return '';
}
$url = get_the_post_thumbnail_url( $post_id, 'full' );
return $url ? esc_url_raw( $url ) : '';
}
/**
* Whether to print frontend hints.
*
* @return bool
*/
private static function should_print_frontend_hints(): bool {
if ( is_admin() || self::is_login_page() || wp_doing_ajax() || wp_doing_cron() ) {
return false;
}
return (bool) apply_filters( 'rx_theme_should_print_resource_hints', true );
}
/**
* Detect login page.
*
* @return bool
*/
private static function is_login_page(): bool {
global $pagenow;
return isset( $pagenow ) && in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ), true );
}
/**
* Check WooCommerce.
*
* @return bool
*/
private static function is_woocommerce_active(): bool {
return class_exists( 'WooCommerce' );
}
/**
* Whether URL needs crossorigin.
*
* @param string $url URL.
* @return bool
*/
private static function needs_crossorigin( string $url ): bool {
$host = wp_parse_url( $url, PHP_URL_HOST );
$site_host = self::get_site_host();
if ( ! $host || ! $site_host ) {
return false;
}
return strtolower( $host ) !== strtolower( $site_host );
}
/**
* Get site host.
*
* @return string
*/
private static function get_site_host(): string {
if ( null !== self::$site_host ) {
return self::$site_host;
}
$host = wp_parse_url( home_url(), PHP_URL_HOST );
self::$site_host = $host ? strtolower( $host ) : '';
return self::$site_host;
}
/**
* Clean unique URLs.
*
* @param array $urls URLs.
* @return array
*/
private static function clean_unique_urls( array $urls ): array {
$clean = array();
foreach ( $urls as $url ) {
if ( empty( $url ) || ! is_string( $url ) ) {
continue;
}
$url = trim( $url );
if ( '' === $url ) {
continue;
}
$clean[] = $url;
}
return array_values( array_unique( $clean ) );
}
/**
* Unique resource items by href + as.
*
* @param array $items Items.
* @return array
*/
private static function unique_resource_items( array $items ): array {
$unique = array();
$seen = array();
foreach ( $items as $item ) {
if ( ! is_array( $item ) || empty( $item['href'] ) ) {
continue;
}
$key = $item['href'] . '|' . ( $item['as'] ?? '' );
if ( isset( $seen[ $key ] ) ) {
continue;
}
$seen[ $key ] = true;
$unique[] = $item;
}
return $unique;
}
/**
* Build HTML attributes.
*
* @param array $attrs Attributes.
* @return string
*/
private static function build_attributes( array $attrs ): string {
$output = array();
foreach ( $attrs as $key => $value ) {
if ( null === $value || false === $value || '' === $value ) {
continue;
}
if ( true === $value ) {
$output[] = esc_attr( $key );
continue;
}
$output[] = sprintf(
'%s="%s"',
esc_attr( $key ),
esc_attr( (string) $value )
);
}
return implode( ' ', $output );
}
}
RX_Theme_Resource_Hints::init();
endif;
Add your own CDN chunks later
In your functions.php, you can add your CDN preload assets like this:
add_filter( 'rx_theme_cdn_preload_assets', function ( $assets ) {
$assets[] = array(
'href' => 'https://static-assets-cdnrx.rxharun.com/_next/static/chunks/02c3irf6-c0dr.js',
'as' => 'script',
'type' => 'application/javascript',
'crossorigin' => 'anonymous',
);
$assets[] = array(
'href' => 'https://static-assets-cdnrx.rxharun.com/_next/static/css/app.css',
'as' => 'style',
'type' => 'text/css',
'crossorigin' => 'anonymous',
);
return $assets;
} );
Add custom CDN host
add_filter( 'rx_theme_cdn_host', function () {
return 'static-assets-cdnrx.rxharun.com';
} );
Add more DNS-prefetch hosts
add_filter( 'rx_theme_static_hosts', function ( $hosts ) {
$hosts[] = 'static-assets-cdnrx.rxharun.com';
$hosts[] = 'cdn.rxharun.com';
$hosts[] = 'assets.rxharun.com';
return $hosts;
} );
Enable prerender carefully
Prerender can increase speed, but it can also waste bandwidth. Enable only when needed:
add_filter( 'rx_theme_prerender_enabled', '__return_true' );
This file is already built so you can expand it later for Next.js chunks, Astro assets, Vite build files, Google Fonts, Cloudflare CDN, WooCommerce, REST API, app JS, CSS, fonts, and hero image preload.