0

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?

  • Do the scripts work correctly if you activate the parent theme? Do both themes run wp_head();? – John P Bloch Apr 19 '11 at 13:04
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');
  • 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 '11 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 '11 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 '11 at 20:38
0

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:http://codex.wordpress.org/Function_Reference/get_stylesheet_directory

  • 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 '11 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy