enqueue.php

I’ll build this as a production-style inc/enqueue.php for your RX theme: assets, preload/preconnect, async/defer/module handling, localize data, Google Fonts option, critical CSS hooks, cache/version helpers, and safe WordPress standards.

Below is a large advanced inc/enqueue.php file for your RX theme. It includes: CSS/JS enqueue, cache-busting, async/defer/module support, preload, preconnect, DNS-prefetch, critical CSS, Google Fonts, WooCommerce conditional assets, comment-reply, AJAX data, REST data, RTL support, admin/editor assets, login assets, CDN hooks, SRI placeholders, and performance filters.

WordPress supports proper enqueueing with wp_enqueue_script() / wp_enqueue_style(), and newer WordPress versions support script loading strategies like defer and async through enqueue args. Resource hints such as DNS-prefetch and preconnect can be added through the wp_resource_hints filter.

Use this file here:

rx-theme/
├── functions.php
├── inc/
│   └── enqueue.php
├── assets/
│   ├── css/
│   │   ├── main.css
│   │   ├── responsive.css
│   │   ├── woocommerce.css
│   │   ├── editor.css
│   │   ├── admin.css
│   │   └── login.css
│   └── js/
│       ├── main.js
│       ├── navigation.js
│       ├── accessibility.js
│       ├── comments.js
│       ├── search.js
│       ├── dark-mode.js
│       └── admin.js

In functions.php, include it like this:

require_once get_template_directory() . '/inc/enqueue.php';

Advanced inc/enqueue.php

<?php
/**
 * RX Theme Advanced Enqueue System
 *
 * Handles frontend, admin, editor, login, preload, preconnect,
 * async/defer/module scripts, RTL styles, WooCommerce assets,
 * critical CSS, CDN assets, and performance resource hints.
 *
 * @package RX_Theme
 */

defined( 'ABSPATH' ) || exit;

if ( ! defined( 'RX_THEME_VERSION' ) ) {
	define( 'RX_THEME_VERSION', wp_get_theme()->get( 'Version' ) ? wp_get_theme()->get( 'Version' ) : '1.0.0';
}

if ( ! defined( 'RX_THEME_DIR' ) ) {
	define( 'RX_THEME_DIR', trailingslashit( get_template_directory() ) );
}

if ( ! defined( 'RX_THEME_URI' ) ) {
	define( 'RX_THEME_URI', trailingslashit( get_template_directory_uri() ) );
}

/**
 * Return file version using filemtime in development and theme version in production.
 */
if ( ! function_exists( 'rx_asset_version' ) ) {
	function rx_asset_version( $relative_path ) {
		$relative_path = ltrim( (string) $relative_path, '/' );
		$file_path     = RX_THEME_DIR . $relative_path;

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG && file_exists( $file_path ) ) {
			return (string) filemtime( $file_path );
		}

		return RX_THEME_VERSION;
	}
}

/**
 * Return asset URI safely.
 */
if ( ! function_exists( 'rx_asset_uri' ) ) {
	function rx_asset_uri( $relative_path ) {
		return RX_THEME_URI . ltrim( (string) $relative_path, '/' );
	}
}

/**
 * Check asset exists before enqueue.
 */
if ( ! function_exists( 'rx_asset_exists' ) ) {
	function rx_asset_exists( $relative_path ) {
		return file_exists( RX_THEME_DIR . ltrim( (string) $relative_path, '/' ) );
	}
}

/**
 * Register one style only if file exists.
 */
if ( ! function_exists( 'rx_register_style_if_exists' ) ) {
	function rx_register_style_if_exists( $handle, $relative_path, $deps = array(), $media = 'all' ) {
		if ( rx_asset_exists( $relative_path ) ) {
			wp_register_style(
				$handle,
				rx_asset_uri( $relative_path ),
				(array) $deps,
				rx_asset_version( $relative_path ),
				$media
			);
			return true;
		}

		return false;
	}
}

/**
 * Register one script only if file exists.
 */
if ( ! function_exists( 'rx_register_script_if_exists' ) ) {
	function rx_register_script_if_exists( $handle, $relative_path, $deps = array(), $strategy = 'defer', $in_footer = true ) {
		if ( ! rx_asset_exists( $relative_path ) ) {
			return false;
		}

		$args = array(
			'in_footer' => (bool) $in_footer,
		);

		if ( in_array( $strategy, array( 'defer', 'async' ), true ) ) {
			$args['strategy'] = $strategy;
		}

		wp_register_script(
			$handle,
			rx_asset_uri( $relative_path ),
			(array) $deps,
			rx_asset_version( $relative_path ),
			$args
		);

		return true;
	}
}

/**
 * Frontend styles and scripts.
 */
if ( ! function_exists( 'rx_enqueue_frontend_assets' ) ) {
	function rx_enqueue_frontend_assets() {

		/**
		 * Main theme stylesheet.
		 */
		wp_enqueue_style(
			'rx-style',
			get_stylesheet_uri(),
			array(),
			rx_asset_version( 'style.css' ),
			'all'
		);

		/**
		 * Main CSS.
		 */
		if ( rx_register_style_if_exists( 'rx-main', 'assets/css/main.css', array( 'rx-style' ) ) ) {
			wp_enqueue_style( 'rx-main' );
		}

		/**
		 * Responsive CSS.
		 */
		if ( rx_register_style_if_exists( 'rx-responsive', 'assets/css/responsive.css', array( 'rx-main' ) ) ) {
			wp_enqueue_style( 'rx-responsive' );
		}

		/**
		 * Print CSS.
		 */
		if ( rx_register_style_if_exists( 'rx-print', 'assets/css/print.css', array(), 'print' ) ) {
			wp_enqueue_style( 'rx-print' );
		}

		/**
		 * RTL CSS.
		 */
		if ( is_rtl() && rx_register_style_if_exists( 'rx-rtl', 'assets/css/rtl.css', array( 'rx-main' ) ) ) {
			wp_enqueue_style( 'rx-rtl' );
		}

		/**
		 * Google Fonts.
		 * Disable with: add_filter( 'rx_enable_google_fonts', '__return_false' );
		 */
		if ( apply_filters( 'rx_enable_google_fonts', true ) ) {
			wp_enqueue_style(
				'rx-google-fonts',
				rx_get_google_fonts_url(),
				array(),
				null,
				'all'
			);
		}

		/**
		 * Main JS.
		 */
		if ( rx_register_script_if_exists( 'rx-main', 'assets/js/main.js', array(), 'defer', true ) ) {
			wp_enqueue_script( 'rx-main' );
		}

		/**
		 * Navigation JS.
		 */
		if ( rx_register_script_if_exists( 'rx-navigation', 'assets/js/navigation.js', array( 'rx-main' ), 'defer', true ) ) {
			wp_enqueue_script( 'rx-navigation' );
		}

		/**
		 * Accessibility JS.
		 */
		if ( rx_register_script_if_exists( 'rx-accessibility', 'assets/js/accessibility.js', array( 'rx-main' ), 'defer', true ) ) {
			wp_enqueue_script( 'rx-accessibility' );
		}

		/**
		 * Search JS.
		 */
		if ( rx_register_script_if_exists( 'rx-search', 'assets/js/search.js', array( 'rx-main' ), 'defer', true ) ) {
			wp_enqueue_script( 'rx-search' );
		}

		/**
		 * Dark mode JS.
		 */
		if ( rx_register_script_if_exists( 'rx-dark-mode', 'assets/js/dark-mode.js', array( 'rx-main' ), 'defer', true ) ) {
			wp_enqueue_script( 'rx-dark-mode' );
		}

		/**
		 * Single post comment script.
		 */
		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}

		if ( is_singular() && rx_register_script_if_exists( 'rx-comments', 'assets/js/comments.js', array( 'rx-main' ), 'defer', true ) ) {
			wp_enqueue_script( 'rx-comments' );
		}

		/**
		 * WooCommerce CSS and JS only when WooCommerce pages are active.
		 */
		if ( rx_is_woocommerce_active() && rx_is_woocommerce_page() ) {
			if ( rx_register_style_if_exists( 'rx-woocommerce', 'assets/css/woocommerce.css', array( 'rx-main' ) ) ) {
				wp_enqueue_style( 'rx-woocommerce' );
			}

			if ( rx_register_script_if_exists( 'rx-woocommerce', 'assets/js/woocommerce.js', array( 'rx-main' ), 'defer', true ) ) {
				wp_enqueue_script( 'rx-woocommerce' );
			}
		}

		/**
		 * Localize global frontend data.
		 */
		if ( wp_script_is( 'rx-main', 'registered' ) || wp_script_is( 'rx-main', 'enqueued' ) ) {
			wp_localize_script(
				'rx-main',
				'rxTheme',
				array(
					'ajaxUrl'       => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
					'restUrl'       => esc_url_raw( rest_url() ),
					'homeUrl'       => esc_url_raw( home_url( '/' ) ),
					'themeUrl'      => esc_url_raw( RX_THEME_URI ),
					'isRtl'         => is_rtl(),
					'isUserLogged'  => is_user_logged_in(),
					'nonce'         => wp_create_nonce( 'rx_theme_nonce' ),
					'restNonce'     => wp_create_nonce( 'wp_rest' ),
					'debug'         => defined( 'WP_DEBUG' ) && WP_DEBUG,
					'currentPostId' => is_singular() ? get_the_ID() : 0,
				)
			);
		}

		/**
		 * Inline CSS variables.
		 */
		$custom_css = rx_get_inline_css_variables();

		if ( ! empty( $custom_css ) ) {
			wp_add_inline_style( 'rx-main', $custom_css );
		}
	}
}
add_action( 'wp_enqueue_scripts', 'rx_enqueue_frontend_assets', 20 );

/**
 * Google Fonts URL.
 */
if ( ! function_exists( 'rx_get_google_fonts_url' ) ) {
	function rx_get_google_fonts_url() {
		$fonts = array(
			'Inter:wght@300;400;500;600;700;800;900',
			'Roboto:wght@300;400;500;700;900',
		);

		$fonts = apply_filters( 'rx_google_fonts_families', $fonts );

		$query_args = array(
			'family'  => implode( '&family=', array_map( 'rawurlencode', $fonts ) ),
			'display' => 'swap',
		);

		return add_query_arg(
			$query_args,
			'https://fonts.googleapis.com/css2'
		);
	}
}

/**
 * CSS variables from Customizer or defaults.
 */
if ( ! function_exists( 'rx_get_inline_css_variables' ) ) {
	function rx_get_inline_css_variables() {
		$primary_color   = get_theme_mod( 'rx_primary_color', '#0b63ce' );
		$secondary_color = get_theme_mod( 'rx_secondary_color', '#0f172a' );
		$text_color      = get_theme_mod( 'rx_text_color', '#111827' );
		$body_bg         = get_theme_mod( 'rx_body_background', '#ffffff' );

		$css  = ':root{';
		$css .= '--rx-primary:' . esc_attr( $primary_color ) . ';';
		$css .= '--rx-secondary:' . esc_attr( $secondary_color ) . ';';
		$css .= '--rx-text:' . esc_attr( $text_color ) . ';';
		$css .= '--rx-body-bg:' . esc_attr( $body_bg ) . ';';
		$css .= '}';

		return apply_filters( 'rx_inline_css_variables', $css );
	}
}

/**
 * WooCommerce active check.
 */
if ( ! function_exists( 'rx_is_woocommerce_active' ) ) {
	function rx_is_woocommerce_active() {
		return class_exists( 'WooCommerce' );
	}
}

/**
 * WooCommerce page check.
 */
if ( ! function_exists( 'rx_is_woocommerce_page' ) ) {
	function rx_is_woocommerce_page() {
		if ( ! rx_is_woocommerce_active() ) {
			return false;
		}

		return (
			function_exists( 'is_woocommerce' ) && is_woocommerce()
		) || (
			function_exists( 'is_cart' ) && is_cart()
		) || (
			function_exists( 'is_checkout' ) && is_checkout()
		) || (
			function_exists( 'is_account_page' ) && is_account_page()
		);
	}
}

/**
 * Add preload links for important local assets.
 */
if ( ! function_exists( 'rx_preload_important_assets' ) ) {
	function rx_preload_important_assets() {
		$preloads = array(
			array(
				'href' => rx_asset_uri( 'assets/css/main.css' ),
				'as'   => 'style',
				'path' => 'assets/css/main.css',
			),
			array(
				'href' => rx_asset_uri( 'assets/js/main.js' ),
				'as'   => 'script',
				'path' => 'assets/js/main.js',
			),
		);

		$preloads = apply_filters( 'rx_preload_assets', $preloads );

		foreach ( $preloads as $item ) {
			if ( empty( $item['href'] ) || empty( $item['as'] ) ) {
				continue;
			}

			if ( ! empty( $item['path'] ) && ! rx_asset_exists( $item['path'] ) ) {
				continue;
			}

			$href        = esc_url( $item['href'] );
			$as          = esc_attr( $item['as'] );
			$type        = ! empty( $item['type'] ) ? ' type="' . esc_attr( $item['type'] ) . '"' : '';
			$crossorigin = ! empty( $item['crossorigin'] ) ? ' crossorigin="' . esc_attr( $item['crossorigin'] ) . '"' : '';

			echo '<link rel="preload" href="' . $href . '" as="' . $as . '"' . $type . $crossorigin . '>' . "\n";
		}
	}
}
add_action( 'wp_head', 'rx_preload_important_assets', 1 );

/**
 * Optional critical CSS.
 */
if ( ! function_exists( 'rx_print_critical_css' ) ) {
	function rx_print_critical_css() {
		if ( ! apply_filters( 'rx_enable_critical_css', true ) ) {
			return;
		}

		$critical_file = RX_THEME_DIR . 'assets/css/critical.css';

		if ( ! file_exists( $critical_file ) ) {
			return;
		}

		$critical_css = file_get_contents( $critical_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents

		if ( empty( $critical_css ) ) {
			return;
		}

		echo "<style id=\"rx-critical-css\">\n";
		echo wp_strip_all_tags( $critical_css );
		echo "\n</style>\n";
	}
}
add_action( 'wp_head', 'rx_print_critical_css', 2 );

/**
 * Resource hints: dns-prefetch, preconnect, preload-style compatible arrays.
 */
if ( ! function_exists( 'rx_resource_hints' ) ) {
	function rx_resource_hints( $urls, $relation_type ) {
		$hints = array(
			'dns-prefetch' => array(
				'//fonts.googleapis.com',
				'//fonts.gstatic.com',
				'//www.google-analytics.com',
				'//www.googletagmanager.com',
			),
			'preconnect' => array(
				array(
					'href'        => 'https://fonts.gstatic.com',
					'crossorigin' => 'anonymous',
				),
				array(
					'href' => 'https://fonts.googleapis.com',
				),
			),
		);

		$hints = apply_filters( 'rx_resource_hints_list', $hints );

		if ( isset( $hints[ $relation_type ] ) && is_array( $hints[ $relation_type ] ) ) {
			$urls = array_merge( $urls, $hints[ $relation_type ] );
		}

		return array_unique( $urls, SORT_REGULAR );
	}
}
add_filter( 'wp_resource_hints', 'rx_resource_hints', 10, 2 );

/**
 * Add SRI, crossorigin, referrerpolicy, module, nomodule to selected scripts.
 */
if ( ! function_exists( 'rx_script_loader_tag' ) ) {
	function rx_script_loader_tag( $tag, $handle, $src ) {
		$script_attributes = array(
			'rx-main' => array(
				'crossorigin' => 'anonymous',
			),
			'rx-navigation' => array(
				'crossorigin' => 'anonymous',
			),
			'rx-module-app' => array(
				'type'        => 'module',
				'crossorigin' => 'anonymous',
			),
			'rx-legacy-app' => array(
				'nomodule'    => true,
				'crossorigin' => 'anonymous',
			),
		);

		$script_attributes = apply_filters( 'rx_script_attributes', $script_attributes, $handle, $src );

		if ( empty( $script_attributes[ $handle ] ) || ! is_array( $script_attributes[ $handle ] ) ) {
			return $tag;
		}

		$attrs = $script_attributes[ $handle ];

		$attr_html = '';

		foreach ( $attrs as $name => $value ) {
			if ( true === $value ) {
				$attr_html .= ' ' . esc_attr( $name );
				continue;
			}

			if ( false === $value || null === $value || '' === $value ) {
				continue;
			}

			$attr_html .= ' ' . esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
		}

		return '<script src="' . esc_url( $src ) . '"' . $attr_html . '></script>' . "\n";
	}
}
add_filter( 'script_loader_tag', 'rx_script_loader_tag', 10, 3 );

/**
 * Add selected style attributes.
 */
if ( ! function_exists( 'rx_style_loader_tag' ) ) {
	function rx_style_loader_tag( $html, $handle, $href, $media ) {
		$style_attributes = array(
			'rx-google-fonts' => array(
				'crossorigin' => 'anonymous',
			),
		);

		$style_attributes = apply_filters( 'rx_style_attributes', $style_attributes, $handle, $href, $media );

		if ( empty( $style_attributes[ $handle ] ) || ! is_array( $style_attributes[ $handle ] ) ) {
			return $html;
		}

		$attr_html = '';

		foreach ( $style_attributes[ $handle ] as $name => $value ) {
			if ( true === $value ) {
				$attr_html .= ' ' . esc_attr( $name );
				continue;
			}

			if ( false === $value || null === $value || '' === $value ) {
				continue;
			}

			$attr_html .= ' ' . esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
		}

		return '<link rel="stylesheet" id="' . esc_attr( $handle ) . '-css" href="' . esc_url( $href ) . '" media="' . esc_attr( $media ) . '"' . $attr_html . ' />' . "\n";
	}
}
add_filter( 'style_loader_tag', 'rx_style_loader_tag', 10, 4 );

/**
 * Register optional module/legacy app build assets.
 *
 * Put files here:
 * assets/app/app.module.js
 * assets/app/app.legacy.js
 * assets/app/app.css
 */
if ( ! function_exists( 'rx_enqueue_app_build_assets' ) ) {
	function rx_enqueue_app_build_assets() {
		if ( ! apply_filters( 'rx_enable_app_build_assets', true ) ) {
			return;
		}

		if ( rx_register_style_if_exists( 'rx-app', 'assets/app/app.css', array( 'rx-main' ) ) ) {
			wp_enqueue_style( 'rx-app' );
		}

		if ( rx_asset_exists( 'assets/app/app.module.js' ) ) {
			wp_register_script(
				'rx-module-app',
				rx_asset_uri( 'assets/app/app.module.js' ),
				array(),
				rx_asset_version( 'assets/app/app.module.js' ),
				array(
					'in_footer' => true,
				)
			);

			wp_enqueue_script( 'rx-module-app' );
		}

		if ( rx_asset_exists( 'assets/app/app.legacy.js' ) ) {
			wp_register_script(
				'rx-legacy-app',
				rx_asset_uri( 'assets/app/app.legacy.js' ),
				array(),
				rx_asset_version( 'assets/app/app.legacy.js' ),
				array(
					'in_footer' => true,
					'strategy'  => 'defer',
				)
			);

			wp_enqueue_script( 'rx-legacy-app' );
		}
	}
}
add_action( 'wp_enqueue_scripts', 'rx_enqueue_app_build_assets', 30 );

/**
 * Optional external CDN assets.
 *
 * Add CDN files using filter:
 *
 * add_filter( 'rx_external_cdn_assets', function( $assets ) {
 *     $assets['styles'][] = array(
 *         'handle' => 'rx-cdn-style',
 *         'src'    => 'https://example.com/style.css',
 *         'deps'   => array(),
 *         'ver'    => '1.0.0',
 *         'media'  => 'all',
 *     );
 *
 *     $assets['scripts'][] = array(
 *         'handle'   => 'rx-cdn-script',
 *         'src'      => 'https://example.com/script.js',
 *         'deps'     => array(),
 *         'ver'      => '1.0.0',
 *         'strategy' => 'defer',
 *     );
 *
 *     return $assets;
 * } );
 */
if ( ! function_exists( 'rx_enqueue_external_cdn_assets' ) ) {
	function rx_enqueue_external_cdn_assets() {
		$assets = array(
			'styles'  => array(),
			'scripts' => array(),
		);

		$assets = apply_filters( 'rx_external_cdn_assets', $assets );

		if ( ! empty( $assets['styles'] ) && is_array( $assets['styles'] ) ) {
			foreach ( $assets['styles'] as $style ) {
				if ( empty( $style['handle'] ) || empty( $style['src'] ) ) {
					continue;
				}

				wp_enqueue_style(
					sanitize_key( $style['handle'] ),
					esc_url_raw( $style['src'] ),
					! empty( $style['deps'] ) ? (array) $style['deps'] : array(),
					isset( $style['ver'] ) ? $style['ver'] : null,
					! empty( $style['media'] ) ? sanitize_key( $style['media'] ) : 'all'
				);
			}
		}

		if ( ! empty( $assets['scripts'] ) && is_array( $assets['scripts'] ) ) {
			foreach ( $assets['scripts'] as $script ) {
				if ( empty( $script['handle'] ) || empty( $script['src'] ) ) {
					continue;
				}

				$args = array(
					'in_footer' => isset( $script['in_footer'] ) ? (bool) $script['in_footer'] : true,
				);

				if ( ! empty( $script['strategy'] ) && in_array( $script['strategy'], array( 'defer', 'async' ), true ) ) {
					$args['strategy'] = $script['strategy'];
				}

				wp_enqueue_script(
					sanitize_key( $script['handle'] ),
					esc_url_raw( $script['src'] ),
					! empty( $script['deps'] ) ? (array) $script['deps'] : array(),
					isset( $script['ver'] ) ? $script['ver'] : null,
					$args
				);
			}
		}
	}
}
add_action( 'wp_enqueue_scripts', 'rx_enqueue_external_cdn_assets', 40 );

/**
 * Remove unnecessary block CSS optionally.
 */
if ( ! function_exists( 'rx_optimize_wordpress_core_assets' ) ) {
	function rx_optimize_wordpress_core_assets() {
		if ( apply_filters( 'rx_disable_wp_block_library_css', false ) ) {
			wp_dequeue_style( 'wp-block-library' );
			wp_dequeue_style( 'wp-block-library-theme' );
			wp_dequeue_style( 'global-styles' );
		}

		if ( apply_filters( 'rx_disable_classic_theme_styles', false ) ) {
			wp_dequeue_style( 'classic-theme-styles' );
		}

		if ( apply_filters( 'rx_disable_dashicons_for_guests', true ) && ! is_user_logged_in() ) {
			wp_dequeue_style( 'dashicons' );
		}
	}
}
add_action( 'wp_enqueue_scripts', 'rx_optimize_wordpress_core_assets', 100 );

/**
 * Block editor assets.
 */
if ( ! function_exists( 'rx_enqueue_block_editor_assets' ) ) {
	function rx_enqueue_block_editor_assets() {
		if ( rx_register_style_if_exists( 'rx-editor', 'assets/css/editor.css', array() ) ) {
			wp_enqueue_style( 'rx-editor' );
		}

		if ( rx_register_script_if_exists( 'rx-editor', 'assets/js/editor.js', array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ), 'defer', true ) ) {
			wp_enqueue_script( 'rx-editor' );
		}
	}
}
add_action( 'enqueue_block_editor_assets', 'rx_enqueue_block_editor_assets' );

/**
 * Admin assets.
 */
if ( ! function_exists( 'rx_enqueue_admin_assets' ) ) {
	function rx_enqueue_admin_assets( $hook_suffix ) {
		if ( rx_register_style_if_exists( 'rx-admin', 'assets/css/admin.css', array() ) ) {
			wp_enqueue_style( 'rx-admin' );
		}

		if ( rx_register_script_if_exists( 'rx-admin', 'assets/js/admin.js', array( 'jquery' ), 'defer', true ) ) {
			wp_enqueue_script( 'rx-admin' );

			wp_localize_script(
				'rx-admin',
				'rxAdmin',
				array(
					'ajaxUrl' => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
					'nonce'   => wp_create_nonce( 'rx_admin_nonce' ),
					'hook'    => sanitize_text_field( $hook_suffix ),
				)
			);
		}
	}
}
add_action( 'admin_enqueue_scripts', 'rx_enqueue_admin_assets' );

/**
 * Login page assets.
 */
if ( ! function_exists( 'rx_enqueue_login_assets' ) ) {
	function rx_enqueue_login_assets() {
		if ( rx_register_style_if_exists( 'rx-login', 'assets/css/login.css', array() ) ) {
			wp_enqueue_style( 'rx-login' );
		}

		if ( rx_register_script_if_exists( 'rx-login', 'assets/js/login.js', array(), 'defer', true ) ) {
			wp_enqueue_script( 'rx-login' );
		}
	}
}
add_action( 'login_enqueue_scripts', 'rx_enqueue_login_assets' );

/**
 * Add body class helper data through inline JS.
 */
if ( ! function_exists( 'rx_inline_body_helper_script' ) ) {
	function rx_inline_body_helper_script() {
		if ( ! wp_script_is( 'rx-main', 'enqueued' ) ) {
			return;
		}

		$script = "
			(function(){
				document.documentElement.classList.remove('no-js');
				document.documentElement.classList.add('js');
				if (window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
					document.documentElement.classList.add('reduced-motion');
				}
			})();
		";

		wp_add_inline_script( 'rx-main', $script, 'before' );
	}
}
add_action( 'wp_enqueue_scripts', 'rx_inline_body_helper_script', 50 );

/**
 * Add no-js class early.
 */
if ( ! function_exists( 'rx_no_js_class_script' ) ) {
	function rx_no_js_class_script() {
		echo "<script>document.documentElement.className=document.documentElement.className.replace('no-js','js');</script>\n";
	}
}
add_action( 'wp_head', 'rx_no_js_class_script', 0 );

/**
 * Add modulepreload for app module.
 */
if ( ! function_exists( 'rx_module_preload' ) ) {
	function rx_module_preload() {
		if ( ! rx_asset_exists( 'assets/app/app.module.js' ) ) {
			return;
		}

		echo '<link rel="modulepreload" href="' . esc_url( rx_asset_uri( 'assets/app/app.module.js' ) ) . '" crossorigin="anonymous">' . "\n";
	}
}
add_action( 'wp_head', 'rx_module_preload', 3 );

/**
 * Preload featured image on single posts for better LCP.
 */
if ( ! function_exists( 'rx_preload_featured_image' ) ) {
	function rx_preload_featured_image() {
		if ( ! is_singular() || ! has_post_thumbnail() ) {
			return;
		}

		$image_id  = get_post_thumbnail_id();
		$image_src = wp_get_attachment_image_src( $image_id, 'full' );

		if ( empty( $image_src[0] ) ) {
			return;
		}

		echo '<link rel="preload" as="image" href="' . esc_url( $image_src[0] ) . '">' . "\n";
	}
}
add_action( 'wp_head', 'rx_preload_featured_image', 4 );

/**
 * Optional: add fetchpriority high to first featured image.
 */
if ( ! function_exists( 'rx_featured_image_fetchpriority' ) ) {
	function rx_featured_image_fetchpriority( $attr, $attachment, $size ) {
		if ( is_singular() && ! is_admin() ) {
			$featured_id = get_post_thumbnail_id();

			if ( $featured_id && isset( $attachment->ID ) && (int) $featured_id === (int) $attachment->ID ) {
				$attr['fetchpriority'] = 'high';
				$attr['loading']       = 'eager';
			}
		}

		return $attr;
	}
}
add_filter( 'wp_get_attachment_image_attributes', 'rx_featured_image_fetchpriority', 10, 3 );

/**
 * Optional lazy loading improvements.
 */
if ( ! function_exists( 'rx_iframe_lazy_loading' ) ) {
	function rx_iframe_lazy_loading( $content ) {
		if ( is_admin() || empty( $content ) ) {
			return $content;
		}

		if ( false === strpos( $content, '<iframe' ) ) {
			return $content;
		}

		$content = preg_replace( '/<iframe(?![^>]+loading=)/i', '<iframe loading="lazy"', $content );

		return $content;
	}
}
add_filter( 'the_content', 'rx_iframe_lazy_loading', 20 );

/**
 * Optional: add decoding async to images in post content.
 */
if ( ! function_exists( 'rx_image_decoding_async' ) ) {
	function rx_image_decoding_async( $content ) {
		if ( is_admin() || empty( $content ) ) {
			return $content;
		}

		if ( false === strpos( $content, '<img' ) ) {
			return $content;
		}

		$content = preg_replace( '/<img(?![^>]+decoding=)/i', '<img decoding="async"', $content );

		return $content;
	}
}
add_filter( 'the_content', 'rx_image_decoding_async', 21 );

/**
 * Register theme support for editor style.
 */
if ( ! function_exists( 'rx_add_editor_styles_support' ) ) {
	function rx_add_editor_styles_support() {
		add_theme_support( 'editor-styles' );

		if ( rx_asset_exists( 'assets/css/editor.css' ) ) {
			add_editor_style( 'assets/css/editor.css' );
		}
	}
}
add_action( 'after_setup_theme', 'rx_add_editor_styles_support' );

/**
 * Optional AJAX example endpoint.
 */
if ( ! function_exists( 'rx_ajax_ping' ) ) {
	function rx_ajax_ping() {
		check_ajax_referer( 'rx_theme_nonce', 'nonce' );

		wp_send_json_success(
			array(
				'message' => esc_html__( 'RX theme AJAX is working.', 'rx-theme' ),
				'time'    => current_time( 'mysql' ),
			)
		);
	}
}
add_action( 'wp_ajax_rx_ping', 'rx_ajax_ping' );
add_action( 'wp_ajax_nopriv_rx_ping', 'rx_ajax_ping' );

/**
 * Optional REST route for theme status.
 */
if ( ! function_exists( 'rx_register_rest_routes' ) ) {
	function rx_register_rest_routes() {
		register_rest_route(
			'rx-theme/v1',
			'/status',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'permission_callback' => '__return_true',
				'callback'            => function () {
					return rest_ensure_response(
						array(
							'theme'   => wp_get_theme()->get( 'Name' ),
							'version' => RX_THEME_VERSION,
							'status'  => 'active',
						)
					);
				},
			)
		);
	}
}
add_action( 'rest_api_init', 'rx_register_rest_routes' );

/**
 * Optional: remove emoji scripts/styles for performance.
 */
if ( ! function_exists( 'rx_disable_emojis' ) ) {
	function rx_disable_emojis() {
		if ( ! apply_filters( 'rx_disable_emojis', true ) ) {
			return;
		}

		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_action( 'init', 'rx_disable_emojis' );

/**
 * Optional: remove emoji CDN from resource hints.
 */
if ( ! function_exists( 'rx_remove_emoji_dns_prefetch' ) ) {
	function rx_remove_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/2/svg/' );
			$urls          = array_diff( $urls, array( $emoji_svg_url ) );
		}

		return $urls;
	}
}
add_filter( 'wp_resource_hints', 'rx_remove_emoji_dns_prefetch', 20, 2 );

/**
 * Optional: disable embeds.
 */
if ( ! function_exists( 'rx_disable_embeds' ) ) {
	function rx_disable_embeds() {
		if ( ! apply_filters( 'rx_disable_embeds', false ) ) {
			return;
		}

		wp_deregister_script( 'wp-embed' );
	}
}
add_action( 'wp_footer', 'rx_disable_embeds' );

/**
 * Optional: preload custom fonts.
 *
 * Put fonts in:
 * assets/fonts/inter.woff2
 */
if ( ! function_exists( 'rx_preload_local_fonts' ) ) {
	function rx_preload_local_fonts() {
		$fonts = array(
			array(
				'path' => 'assets/fonts/inter.woff2',
				'type' => 'font/woff2',
			),
		);

		$fonts = apply_filters( 'rx_preload_local_fonts', $fonts );

		foreach ( $fonts as $font ) {
			if ( empty( $font['path'] ) || ! rx_asset_exists( $font['path'] ) ) {
				continue;
			}

			$type = ! empty( $font['type'] ) ? $font['type'] : 'font/woff2';

			echo '<link rel="preload" href="' . esc_url( rx_asset_uri( $font['path'] ) ) . '" as="font" type="' . esc_attr( $type ) . '" crossorigin="anonymous">' . "\n";
		}
	}
}
add_action( 'wp_head', 'rx_preload_local_fonts', 5 );

/**
 * Optional: CDN host replacement for local assets.
 *
 * In wp-config.php:
 * define( 'RX_CDN_URL', 'https://static-assets-cdnrx.rxharun.com' );
 */
if ( ! function_exists( 'rx_cdn_replace_asset_url' ) ) {
	function rx_cdn_replace_asset_url( $src ) {
		if ( ! defined( 'RX_CDN_URL' ) || empty( RX_CDN_URL ) ) {
			return $src;
		}

		$site_url = get_site_url();

		if ( 0 === strpos( $src, $site_url ) ) {
			$src = str_replace( $site_url, untrailingslashit( RX_CDN_URL ), $src );
		}

		return $src;
	}
}

if ( apply_filters( 'rx_enable_cdn_rewrite', false ) ) {
	add_filter( 'style_loader_src', 'rx_cdn_replace_asset_url', 20 );
	add_filter( 'script_loader_src', 'rx_cdn_replace_asset_url', 20 );
}

/**
 * Optional debug comment for admins.
 */
if ( ! function_exists( 'rx_assets_debug_comment' ) ) {
	function rx_assets_debug_comment() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
			return;
		}

		echo "\n<!-- RX Theme assets loaded. Version: " . esc_html( RX_THEME_VERSION ) . " -->\n";
	}
}
add_action( 'wp_head', 'rx_assets_debug_comment', 99 );

Good optional filters for functions.php

You can add these in functions.php only when needed:

/**
 * Disable Google Fonts.
 */
// add_filter( 'rx_enable_google_fonts', '__return_false' );

/**
 * Disable WordPress block CSS on frontend.
 */
// add_filter( 'rx_disable_wp_block_library_css', '__return_true' );

/**
 * Enable CDN rewrite.
 */
// add_filter( 'rx_enable_cdn_rewrite', '__return_true' );

/**
 * Add external CDN CSS/JS.
 */
add_filter( 'rx_external_cdn_assets', function( $assets ) {

	/*
	$assets['styles'][] = array(
		'handle' => 'rx-example-cdn-css',
		'src'    => 'https://example.com/example.css',
		'deps'   => array(),
		'ver'    => '1.0.0',
		'media'  => 'all',
	);

	$assets['scripts'][] = array(
		'handle'   => 'rx-example-cdn-js',
		'src'      => 'https://example.com/example.js',
		'deps'     => array(),
		'ver'      => '1.0.0',
		'strategy' => 'defer',
	);
	*/

	return $assets;
} );

My dear friend, this is a strong RX Theme enqueue foundation. The most important rule: do not enqueue every file blindly. Add many options, yes, but load assets conditionally so your website stays fast.

Leave a Reply

Your email address will not be published. Required fields are marked *