0

I'm having a lot of trouble registering scripts located in a child theme folder:

This works fine:

function register_scripts() {
    wp_register_script( 'newsletter', get_template_directory_uri() . '/scripts/scripts.js', array( 'jquery-migrate' ), null );
}
add_action( 'init', 'register_scripts' );

However it points to the parent theme folder and not the child, when I try to point to the child theme folder, nothing at all happens:

function register_scripts() {
    wp_register_script( 'newsletter', get_stylesheet_directory_uri() . '/scripts/scripts.js', array( 'jquery-migrate' ), null );
}
add_action( 'init', 'register_scripts' );

What am I doing wrong?

  • 2
    Can you clarify "nothing at all happens"? Is the script registered with a wrong url or not at all? – kraftner May 15 '14 at 13:36
  • Hi Kraftner, what happens is that the script doesn't appear on the html at all. – Lisandro Vaccaro May 15 '14 at 13:40
4

I think that none the options you has posted actually works. You are only registering the script, you need to enqeue them. Also, you should use wp_enqueue_scripts() action hook instead of init().

    function register_scripts() {
        wp_register_script( 'newsletter', get_stylesheet_directory_uri() . '/scripts/scripts.js', array( 'jquery-migrate' ), null );
        wp_enqeue('newsletter');
    }
   add_action( 'wp_enqueue_scripts', 'register_scripts' );
3

I don't see how the first block of code works and not the second since in both cases all you are doing is registering the script, not enqueueing it. If you don't enqueue nothing will show up on the front end at all.

function register_scripts() {
  wp_register_script( 'newsletter', get_stylesheet_directory_uri() . '/scripts/scripts.js', array( 'jquery-migrate' ), null );
  wp_enqueue_script('newsletter'); // you need this
}
add_action( 'wp_enqueue_scripts', 'register_scripts' );

The other possibility is that your enqueueing code is elsewhere and is running before the script is registered.

Also note that the correct hook to be enqueueing scripts is wp_enqueue_scripts, not init.

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

http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts

That should be what other code assumes, so you may be having trouble with timing because of that.

  • Extended explanation than mine ;) – cybmeta May 15 '14 at 14:10
  • @cybnet : didn't even see your answer-- exactly the same timestamp :) – s_ha_dum May 15 '14 at 14:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.