Forums

What is the best/proper hook for wp_enqueue_script? (2 posts)

  1. Brayne
    Member
    Posted 1 year ago #

    I've seen function call examples using all 3 of these action hooks to try and accomplish the same thing.

    add_action('init', 'my_enqueue_js_function');
    add_action('template_redirect', 'my_enqueue_js_function');
    add_action('wp_head', 'my_enqueue_js_function');

    If I'm only using a script for a front-end GUI, Would it be a better practice to use 'init' to register the scripts and then wp_head to enqueue the scripts? What, if any advantage is there to using template_redirect as opposed to wp_head?

    Thanks in advance to whoever could shed some light.

  2. Brayne
    Member
    Posted 1 year ago #

    I see that the codex recommends 'init'. Just curious as to why some folks would use 'template_redirect' or 'wp_head' instead?

    Also, I've seen some plugin authors use 2 different action calls, 1. to register the script and 2. to enqueue. Isn't it more efficient to take care of both in the same action call?

Topic Closed

This topic has been closed to new replies.

About this Topic