Load JavaScript or CSS file from a WordPress child theme

WordPress
// PHP

What it does

If you want to load a script or stylesheet from within a child theme in WordPress, you must use get_stylesheet_directory_uri() in your path to the file, otherwise WordPress will look in the parent theme’s directory.

The Code

// Load custom script from child theme

function guyPrimavera_script_from_child_theme() {
    wp_enqueue_script(
        'my_script',                                         // Script name
        get_stylesheet_directory_uri() . '/js/my_script.js', // Script path (note the 'get_stylesheet_directory_uri')
        array( 'jquery' )                                    // Dependencies
    );
}

add_action( 'wp_enqueue_scripts', 'guyPrimavera_script_from_child_theme' ); // Call your function

How To Use It

Paste this snippet into your theme's functions.php or in a WordPress plugin.

Menu