• Resolved ken90

    (@ken90)


    Hi, I’m learning how things work in wordpress and recently I’ve learned something about enqueuing and registering scripts. Thing I don’t get is why do we need to register a script and then enqueue it when we can just use enqueue function? Only practical use I see in this is deregistering and then registering a different version of jquery. To put it short is there any point in writing

    function my_jquery_script()
    {
       wp_register_script( 'my-script', get_template_directory_uri() . '/js/my-script.js', array( 'jquery' ) );
     wp_enqueue_script( 'my-script' );
    }
    add_action( 'wp_enqueue_scripts', 'my_jquery_script' );

    when we could just do this:

    function my_jquery_script()
    {
      wp_enqueue_script( 'my-script', get_template_directory_uri() . '/scripts/my-script.js', array( 'jquery'), '1.0', false ); ;
    }
        add_action('wp_enqueue_scripts', 'my_jquery_script');
Viewing 1 replies (of 1 total)
  • Let’s say you have six different scripts. Some of those scripts load on only a couple of pages, some load on all pages, some on half of your pages, etc. And there is a lot of overlap. You only want to load the scripts on the pages where they are needed, so you are putting them inside conditionals– if (is_page()) {...}, for example.

    If you are using wp_enqueue_script without wp_register_script you have to write out the whole set of parameters every time you enqueue your script. If you register the script, you can enqueue it using the shorthand syntax.

    If you need to change the version of the script, you can change it in one place instead of two or three or four places.

    And as you noted, you can de-register a script, which can be convenient.

Viewing 1 replies (of 1 total)

The topic ‘Register and enqueue a script’ is closed to new replies.