• Resolved cag8f

    (@cag8f)


    Hi all. I’m new to JavaScript and using it with WordPress. I understand that JavaScript files should be implemented using the function wp_enqueue_script(). But the description of the function reads,

    Registers the script if $src provided (does NOT overwrite), and enqueues it.

    What exactly is the difference between registering the script and enqueuing it? And how do they relate to the ability of my PHP code to include the JS file?

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • In simplest terms, registering it only tells WordPress about it, enqueuing it adds it to the website.

    If you register it you would still need to enqueue it ( by just supplying the name you registered it with, without supplying the URL ).

    You can just go ahead and use the wp_enqueue_script()

    Thread Starter cag8f

    (@cag8f)

    OK thanks for that. Am I correct then, that in order to be able to include a script on a page, it first needs to be enqueued and registered? If that is correct, what is the reason for this? It sounds like a security measure.

    I also see that wp_enqueue_script() will take care of registering the script if the $src parameter is provided.

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    Not enqueued AND registered (by two different function calls), since enqueuing serves to register if $src is supplied. Then why have register at all? In cases where you need to reference the script by tag even though you do not wish to always enqueue it. Mainly so you can reference it in dependency arguments. This serves to enqueue it without explicitly calling wp_enqueue_script() for that particular script.

    When you enqueue, it is always loaded by the page. This is not always desirable for every request. By registering, it is available to be enqueued, but is not actually enqueued unless it is needed through some other mechanism like dependencies. It’s just a different way to organize scripts. You don’t need it if you don’t want to, enqueuing will always work.

    Thread Starter cag8f

    (@cag8f)

    Then why have register at all? In cases where you need to reference the script by tag even though you do not wish to always enqueue it. Mainly so you can reference it in dependency arguments.

    OK this is illuminating–thanks for that.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Difference between ‘register’ and ‘enqueue’ a script?’ is closed to new replies.