Classic theme assets build

From Lonsdale201/wp-agent-skills · View interactive version

Classic theme assets build is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Build or audit frontend asset loading for classic PHP WordPress themes on WP 7.1. Covers `wp_enqueue_scripts`, child-theme-safe `get_theme_file_uri()` and `get_theme_file_path()`, cache-busting with `filemtime()`, classic scripts and script modules, loading strategy/fetch priority, conditional enque

How to use this skill

Skill file

Classic Theme Assets and Build Output

Use this when adding or reviewing CSS, JavaScript, fonts, images, build artifacts, cache busting, or conditional frontend assets in a classic PHP theme.

This skill is for the frontend theme layer. Admin, editor, and block-specific asset loading are separate concerns.

When to Use This Skill

Hook and Path Rules

Frontend theme assets load on wp_enqueue_scripts.

add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_assets' );

function mytheme_enqueue_assets() {
	$theme = wp_get_theme();

	wp_enqueue_style(
		'mytheme-style',
		get_stylesheet_uri(),
		array(),
		$theme->get( 'Version' )
	);
}

Rules:

Cache Busting

Use filemtime() for local built files when the file exists. Fall back to the theme version.

function mytheme_asset_version( $relative_path ) {
	$path = get_theme_file_path( $relative_path );

	if ( file_exists( $path ) ) {
		return (string) filemtime( $path );
	}

	return wp_get_theme()->get( 'Version' );
}

Example:

wp_enqueue_style(
	'mytheme-main',
	get_theme_file_uri( 'assets/css/main.css' ),
	array(),
	mytheme_asset_version( 'assets/css/main.css' )
);

Rules:

JavaScript Loading

WP 7.1 retains the modern $args array for wp_enqueue_script() (introduced

in 6.3). It supports strategy, in_footer, and, since 6.9,

fetchpriority.

wp_enqueue_script(
	'mytheme-navigation',
	get_theme_file_uri( 'assets/js/navigation.js' ),
	array(),
	mytheme_asset_version( 'assets/js/navigation.js' ),
	array(
		'strategy'  => 'defer',
		'in_footer' => true,
	)
);

Rules:

Script modules

Use wp_enqueue_script_module() for a real ES module/import-map graph rather

than printing a hand-written <script type="module">. Dependencies are module

IDs; array entries can mark import as static or dynamic. Since WordPress

6.9 the args array supports in_footer and fetchpriority, and WordPress 7.0+

supports wp_set_script_module_translations().

Do not list classic script handles as module dependencies. If a classic script

must dynamically import registered modules, use the module_dependencies

classic-script argument available in WordPress 7.0+, or redesign one side of

the boundary. Use the wp-plugin-assets-loading skill for the complete module

contract and compatibility pattern.

Inline Data

Use wp_add_inline_script() for boot data and encode PHP data with wp_json_encode().

$data = array(
	'ajaxUrl' => admin_url( 'admin-ajax.php' ),
	'nonce'   => wp_create_nonce( 'mytheme_public_action' ),
);

wp_add_inline_script(
	'mytheme-navigation',
	'window.MyTheme = ' . wp_json_encode( $data ) . ';',
	'before'
);

Rules:

Conditional Enqueues

Load assets only where they are needed.

if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
	wp_enqueue_script( 'comment-reply' );
}

if ( is_page_template( 'page-templates/landing.php' ) ) {
	wp_enqueue_style(
		'mytheme-landing',
		get_theme_file_uri( 'assets/css/landing.css' ),
		array( 'mytheme-main' ),
		mytheme_asset_version( 'assets/css/landing.css' )
	);
}

Good gating signals:

Avoid running expensive queries just to decide whether to enqueue a file.

RTL, Preload, and Resource Hints

If the theme ships an RTL replacement stylesheet, register it with style data:

wp_style_add_data( 'mytheme-main', 'rtl', 'replace' );

Use resource hints sparingly:

For fonts:

Build Output Layout

A practical classic theme layout:

mytheme/
|-- assets/
|   |-- src/
|   |   |-- js/
|   |   `-- css/
|   |-- css/
|   |   `-- main.css
|   |-- js/
|   |   `-- navigation.js
|   `-- manifest.json

Rules:

Review Checklist

Common Mistakes

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.