[theme-reviewers] Which one get processed first? print_scripts or enqueue_scripts?

Otto otto at ottodestruct.com
Tue Jan 22 18:26:50 UTC 2013


The question itself is slightly confusing, because it kinda depends on
the purpose...

For the front-end of the site, the wp_print_scripts action is actually
executed from the wp_print_head_scripts() function.

The wp_print_head_scripts() function is actually hooked to the wp_head
action, like so:
add_action( 'wp_head', 'wp_print_head_scripts', 9 );

Same goes for wp_enqueue_scripts. The wp_enqueue_scripts action is in
the wp_enqueue_scripts() function which is called like so:
add_action( 'wp_head', 'wp_enqueue_scripts', 1 );

So the answer is different depending on whether you consider the
action to have occurred when the action is called or when the action
has returned.

The long and the short of it is that both enqueuing and printing of
scripts happen inside the wp_head() call. So the question of ordering
isn't something that external code really needs to consider. Instead,
you need to know that you should add scripts on wp_enqueue_scripts,
and remove scripts on wp_print_scripts (as this is the last possible
moment to remove them before they get printed to the output).

-Otto


On Tue, Jan 22, 2013 at 12:16 PM, Michael Fields <michael at mfields.org> wrote:
> Here's a simple way to test this:
>
> <?php
> function mfields_test_some_hooks() {
>         static $i = 1;
>         var_dump( $i . ' - ' . current_filter() );
>
>         $i++;
> }
>
> add_action( 'wp_head',            'mfields_test_some_hooks' );
> add_action( 'wp_print_scripts',   'mfields_test_some_hooks' );
> add_action( 'wp_enqueue_scripts', 'mfields_test_some_hooks' );
> ?>
>
> I get the following order:
>
> 1 - wp_enqueue_scripts
> 2 - wp_print_scripts
> 3 - wp_head
> _______________________________________________
> theme-reviewers mailing list
> theme-reviewers at lists.wordpress.org
> http://lists.wordpress.org/mailman/listinfo/theme-reviewers


More information about the theme-reviewers mailing list