Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

wp_enqueue_scripts

The right hook for loading stylesheets and scripts on the front end with wp_enqueue_style() and wp_enqueue_script().

  • Type Action
  • From WordPress
  • Since 2.8.0
  • Fired in wp-includes/script-loader.php

When it fires

WordPress fires wp_enqueue_scripts from wp_head at priority 1, so it runs on every front-end page just before styles and scripts are printed. The main query has already run, so conditional tags such as is_page() and is_singular() work.

Despite the name, it's for stylesheets as well as scripts. It doesn't fire in the admin area or on the login page; use admin_enqueue_scripts and login_enqueue_scripts there.

Parameters

None. Your callback takes no arguments.

Example: Add CSS to one page only

PHP
add_action( 'wp_enqueue_scripts', function () {
	if ( ! is_page( 'contact' ) ) {
		return;
	}
	wp_register_style( 'my-contact-page', false, array(), '1.0.0' );
	wp_enqueue_style( 'my-contact-page' );
	wp_add_inline_style( 'my-contact-page', '.entry-title { text-align: center; }' );
} );

Good to know

  • Registering a handle with false as the source, as in the example, gives you somewhere to attach inline CSS without a separate file.
  • To remove a theme or plugin file, call wp_dequeue_style() or wp_dequeue_script() with its handle at a later priority, such as 100, so it has been enqueued first.
  • For styles inside the block editor, use enqueue_block_assets or add_editor_style() instead.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.