[wp-hackers] Re: wp_enqueue_script Question

Ozh ozh at planetozh.com
Thu Mar 27 17:10:26 GMT 2008


>I'm not sure that's correct - I've always used:
>
>add_action('wp_head',  wp_enqueue_script('jquery') );
>
>Which _just works_.  Putting it inside a function (as Austin did)
>never works for me.

It just works probably because jQuery is loaded by default or by another 
plugin.

Proof of concept: add and activate this plugin

<?php
/*
Plugin Name: An Enqueue Test
Plugin URI: #
Description: Blah
Author: Ozh
Version: 0.0
Author URI: #
*/
add_action ('init', 'enqueue_test');
function enqueue_test() {
        wp_enqueue_script(
                'enqueue_test',
                'http://blah/enqueue.js'
        );
}

?>

This will add into the <head> a call to http://blah/enqueue.js

Now, modify the add_action with 'wp_head' (or with 'admin_header' if 
you're working in the admin area) instead of 'init' : won't add anything.

As I understand it (didn't really dig into these parts of code) you need 
to pick a hook before any output is started. 'init' or 'plugins_loaded' 
are fine, 'template_redirect' is fine too for the blog area (but obviously 
not the admin area)
'wp_head' is too late.

Ozh


More information about the wp-hackers mailing list