0

If this works (and it's the way people do in a bunch of themes around the web):

<html>
<head>
    <!-- stuff -->
    <?php wp_enqueue_script('jquery'); ?>
    <?php wp_head(); ?>
</head>
<!-- more stuff -->

Why is it recommended to use a hook to enqueue scripts?

add_action('wp_enqueue_scripts', 'my_enqueue_scripts');
function my_enqueue_scripts() {
    wp_enqueue_script('jquery');
}

1 Answer 1

4

Enqueue mechanics and suggested ways to use them serve flexibility and interoperability. Simply put consider what would be the easiest way for third party code to modify mechanics of yours?

Hardcoding things in template often makes editing that template the only viable option. Manipulating hooks is typically more convenient and less problematic than that.

On larger scale there are different opinions and approaches on balance of things between templates and hooks, however enqueue is strongly recommended to be used via hooks in all cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.