How do I get a child theme to load scripts from the parent theme?

Pretty new to WordPress functions, so apologies if this is basic stuff.

I’ve designed a parent theme that registers and enqueues two scripts like so:

Read More
    if ( !function_exists( 'scherzo_scripts' ) ) :

    function scherzo_scripts() {

        if (!is_admin()) {

        wp_register_script( 'html5shiv', get_template_directory_uri() . '/js/html5.js');
        wp_register_script( 'css3mediaqueries', get_template_directory_uri() . '/js/css3-mediaqueries.js');
        wp_enqueue_script('html5shiv');
        wp_enqueue_script('css3mediaqueries');

        }
    }

    add_action('init', 'scherzo_scripts');

endif;

I’m finding that if I install a child theme the scripts don’t run. To try and force them to run I create a new functions.php file in the child theme folder and add:

function scherzo_scripts() {

        if (!is_admin()) {

            wp_register_script( 'html5shiv', get_template_directory_uri() . '/js/html5.js');
            wp_register_script( 'css3mediaqueries', get_template_directory_uri() . '/js/css3-mediaqueries.js');
            wp_enqueue_script('html5shiv');
            wp_enqueue_script('css3mediaqueries');

        }

    }

    add_action('init', 'scherzo_scripts');

But no joy. Am I doing something wrong?

Related posts

Leave a Reply

3 comments

  1. functions.php file of the child theme is loaded before the functions.php of the parent theme so it’s best to run the code at the hook after both themes are loaded. At the earliest at after_setup_theme hook so Change:

     add_action('init', 'scherzo_scripts');
    

    to:

    add_action('after_setup_theme', 'scherzo_scripts');
    
  2. 2023 Update:
    WordPress Docs

    get_stylesheet_directory(): string
    Retrieves stylesheet directory
    path for the active theme.

    • The returning path does not contain a trailing slash.
    • An example output of get_stylesheet_directory() is
      /home/user/public_html/wp-content/themes/my_theme
    • In the event a child theme is being used, that is the directory that will be returned, not the parent theme directory (use
      get_template_directory() instead if you want the parent directory).
    • To retrieve the URI of the stylesheet directory use get_stylesheet_directory_uri()
    • To retrieve the path of a parent theme, use get_template_directory()
  3. I’m not sure why your scripts won’t load when you install a child theme.

    However, when you want to load scripts from a child theme you’ll want to use get_stylesheet_directory() instead of get_template_directory_uri().

    The differnece between the two is that get_template_directory_uri() will always refer to the parent theme, whereas get_stylesheet_directory() will refer to the directory of the stylesheet being used. When using a Child theme it automatically will fetch the Child themes url.

    Hope this helps, good luck!

    See: https://developer.wordpress.org/reference/functions/get_stylesheet_directory/