WordPress enqueue script

wp_enqueue_script()

Correctly connect script (JavaScript) file onto the page.

Use this function to connect the js file is important because as you can in the future without unnecessary problems combine JS files into one. Also, in some cases, get rid of the scripts conflicts when dependent script connects after which he depends.

If the script is already registered with wp_register_script (), then to connect it to wp_enqueue_script (), you need to specify only the script shortcut (in the first parameter).

wp_enqueue_script()

adds the script only if it has not yet been added, and other scripts on which it depends are registered. Dependent scripts are added automatically.

If the script has already been registered by a wp_register_script (), then connect it to the

wp_enqueue_script() you need only specify the label of the script (the first parameter).

// jquery recorded by default in the WP.
// Therefore, it is sufficient to connect one line:
wp_enqueue_script('jquery');

If the script is not registered, you can register and connect one function

wp_enqueue_script('newscript', get_template_directory_uri() . '/js/custom_script.js');

With version 3.3. wp_enqueue_script () can be called during the page generation. In this case, the called script will be connected in the basement, at the time of the events wp_footer operation.

The function is usually called during the following events (hung on hooks):

  • wp_enqueue_scripts – for the external part of the site;
    admin_enqueue_scripts – to admin panel;
    login_enqueue_scripts – login page.

About The Author

Leave A Comment?