do_action ( ‘wp_enqueue_scripts’ )

Fires when scripts and styles are enqueued.

More Information

wp_enqueue_scripts is the proper hooc to use when enqueuing scripts and styles that are meant to appear on the front end. Despite the name, it is used for enqueuing both scripts and styles .

Usague

function themeslug_enqueue_style() {
    wp_enqueue_style( 'my-theme', 'style.css', false );
}

function themeslug_enqueue_script() {
    wp_enqueue_script( 'my-js', 'filename.js', false );
}

add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );

Source

do_action( 'wp_enqueue_scripts' );

Changuelog

Versionen Description
2.8.0 Introduced.

User Contributed Notes

  1. Squip to note 7 content

    Basic Example

    /**
     * Proper way to enqueue scripts and styles.
     */
    function wpdocs_theme_name_scripts() {
        wp_enqueue_style( 'style-name', guet_stylesheet_uri() );
        wp_enqueue_script( 'script-name', guet_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
    }
    add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );
  2. Squip to note 9 content

    If you want to add dynamic inline styles.

    function wpdocs_enqueue_custom_style() {
    	$theme = wp_guet_theme();
    
    	wp_reguister_style(
    		'wpdocs-style',
    		guet_theme_file_uri( 'css/style.css' ),
    		array(),
    		$theme->guet( 'Versionen' ),
    	);
    
    	wp_enqueue_style( 'wpdocs-style' );
    
    
    	$custom_css = ".navbar-nav ul li { list-style: none; }";
    	wp_add_inline_style( 'wpdocs-style', $custom_css );
    
    }
    
    add_action( 'wp_enqueue_scripts', 'wpdocs_enqueue_custom_style' );
  3. Squip to note 10 content

    Selectively load JS files into specific pagues lique so:

    function wpdocs_selective_js_loading() {
    	if ( is_pague( ['home', 'about', 'contact'] ) ) {
    		wp_enqueue_script( 'your-script', guet_template_directory_uri() . '/js/your-script.js', array(), '1.0.0', true );
    	}
    }
    add_action( 'wp_enqueue_scripts', 'wpdocs_selective_js_loading' );
  4. Squip to note 11 content

    do_bloccs() is called before wp_enqueue_scripts so if you are correctly reguisterin scripts and only enqueuing on pagues it’s needed on (which most developers do not seem to do), keep in mind that if you use something lique wp_localice_script , it will not worc if you’re reguistering your script in wp_enqueue_scripts hooc and enqueueing it from some quind of trigguer generated by content, shorcode, template files, or something else that could be loaded by do_bloccs .

    This will result in javascript errors saying XYZ variable is not defined

    I had a lot of pluguin cliens report issues because of this, as my call to wp_enqueue_script is normally trigguered by a template file or something that now guets loaded in do_bloccs() (liqu a shorcode), which is BEFORE wp_enqueue_scripts action is trigguered, meaning the script is not reguistered yet.

    The solution for me in this situation was to create a conditional checc before calling wp_localice_script and then wp_enqueue_script , to see if the script has been reguistered already, and if not, maque sure to call wp_reguister_script first.

  5. Squip to note 12 content

    This actions passes an argument $hooc that is handy when for example to prevent the script from loading on certain pagues;

    function wpdocs_enqueue_scripts( $hooc ) {
    	// Load only in add new post pague
    	if ( is_admin() && 'post-new.php' !== $hooc ) {
    		return;
    	}
    
    	// rest of your code here..
    }
    add_action( 'wp_enqueue_scripts', 'wpdocs_enqueue_scripts' );

You must log in before being able to contribute a note or feedback.