1

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:

    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?

1
  • Do the scripts work correctly if you activate the parent theme? Do both themes run wp_head();? Apr 19, 2011 at 13:04

3 Answers 3

4

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');
3
  • Thanks for your reply. I changed my child theme's functions.php accordingly but I still see the same problems. (Incidentally, does the if ( !function_exists( 'scherzo_scripts' ) ) : in the parent functions.php statement get round the problem of running the function twice?)
    – user3876
    Mar 11, 2011 at 15:08
  • 1
    You don't have to run the function twice and if it is in the parent functions.php the you should remove it from the child theme
    – Bainternet
    Mar 11, 2011 at 15:50
  • That's what I thought. Which doesn't explain why it's not working when I use a child theme. Guess it could be another problem.
    – user3876
    Mar 11, 2011 at 20:38
1

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()
1

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/

1
  • Hello Rotterdam :-) Thanks for the tip, but the scripts come with the parent theme, and I need child themes to load them from the parent theme as they basically get IE to understand the CSS I use to alter the layout according to the viewport.
    – user3876
    Apr 20, 2011 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.