3

For some reason my jQuery script is not being registered, I believe I am doing it correctly but maybe I'm missing something. Here's my code:

/*registering script*/
    function register_my_script(){
        wp_register_script('alliance_script', get_template_directory_uri() . '/js/script.js');
    }
    add_action('enqueue_scripts', 'register_my_script');
1
  • 1
    you missed "wp". use "wp_enqueue_scripts" without "enqueue_scripts" Commented Jul 7, 2017 at 8:30

2 Answers 2

2

In WordPress used wp_enqueue_scripts() to register your scripts.

For more information click here

add_action('wp_enqueue_scripts', 'my_enqueue_scripts');

function my_enqueue_scripts() {

   wp_register_script( 'alliance_script', get_template_directory_uri() . '/js/script.js');
   wp_enqueue_script( 'alliance_script' );
}

wp_register_script vs. wp_enqueue_script

  • wp_register_script() function makes your scripts available for use while wp_enqueue_script() functions loads the script to the theme/plugin.
  • You can register without enqueuing. But to load the script on the page, you need to enqueue. If you register the script, you can then enqueue it by its handle alone. If you don’t register it ahead of time, you will need to provide the full parameters in the enqueue function.
  • You can skip the register function for the scripts that you are going to enqueue right away. Also, you don’t need to register scripts which are included in WordPress.
Sign up to request clarification or add additional context in comments.

2 Comments

whats the difference between register and enqueue? register didn't work for me until I used enqueue
1

I think the problem is that you're using 'enqueue_scripts' as action, which should be 'wp_enqueue_scripts'.

function register_my_script(){
    wp_register_script('alliance_script', get_template_directory_uri() . '/js/script.js');
}
add_action('wp_enqueue_scripts', 'register_my_script');

2 Comments

wp_register_script() function makes your scripts available for use while wp_enqueue_script() functions loads the script to the theme/plugin.
Indeed, if you want to run them you should do that, but the question specifically mentioned registering scripts, hence my answer. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.