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

Abhik Biswas abhik at itsabhik.com
Tue Jan 22 18:54:35 UTC 2013


Thanks Otto,
That's exactly what I was looking for.
In fact, I am currently looking at your post here
http://ottopress.com/2010/passing-parameters-from-php-to-javascripts-in-plugins/

Chip,
I am trying to put my theme's settings as inline javascript variables to
use on the script I enqueue later using wp_enqueue_scripts hook.


On Wed, Jan 23, 2013 at 12:15 AM, Otto <otto at ottodestruct.com> wrote:

> That is probably not the right way. The "right" way depends on the
> contents of your script code there, actually.
>
>
> If you're needing script dependencies, where you have to load one
> external script before another, then wp_register_script and
> wp_enqueue_script support this themselves. Just register all the .js
> links and use the $deps argument to make them dependent on the proper
> scripts. Like jquery-ui is dependent on jquery, for example. Then the
> script printer will put them all in the right order.
>
>
> If you need to add inline variables for scripts to use, then
> wp_localize_script will do that. For example, I can register a script
> and pass PHP variables to it as named parameters, like so:
>
> wp_register_script('example', 'http://example/whatever.js');
>
> wp_localize_script('example', 'Example', array(
>   'examplea' => 'foo',
>   'exampleb' => 'bar',
> ) );
>
> When I then enqueue the 'example' script later, the script printer
> will recognize the localization as a dependency, and print this out in
> a script tag:
>
> var Example = {"examplea":"foo","exampleb":"bar"}
>
> The external .js script can then access these variables as
> Example.examplea and Example.exampleb.
>
> wp_localize_script() was meant for allowing the Internationalization
> code to be able to pass translated strings into the javascript files,
> but it doesn't have to be limited to that. You can pass any variable
> data you can put into the array in this manner.
>
> -Otto
>
>
>
> On Tue, Jan 22, 2013 at 12:34 PM, Abhik Biswas <abhik at itsabhik.com> wrote:
> > Okay,
> > So, what if I need to put something *before* my enqueued scripts, what
> > should I use?
> >
> > Is this the right way?
> >
> > function my_scripts() { ?>
> > <script type="text/javascript">
> > ........
> > </scripts>
> >
> > <?php }
> > add_action ('wp_enqueue_scripts', 'my_scripts', 5 );
> >
> >
> >
> >
> >
> > function my_enqueued_scripts() {
> >
> >  wp_enqueue_script ('myscript', ...... myotherscript.js....., '');
> >
> > }
> > add_action ('wp_enqueue_scripts', 'my_enqueued_script', );
> >
> >
> > On Tue, Jan 22, 2013 at 11:56 PM, Otto <otto at ottodestruct.com> wrote:
> >>
> >> 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
> >> _______________________________________________
> >> theme-reviewers mailing list
> >> theme-reviewers at lists.wordpress.org
> >> http://lists.wordpress.org/mailman/listinfo/theme-reviewers
> >
> >
> >
> > _______________________________________________
> > theme-reviewers mailing list
> > theme-reviewers at lists.wordpress.org
> > http://lists.wordpress.org/mailman/listinfo/theme-reviewers
> >
> _______________________________________________
> theme-reviewers mailing list
> theme-reviewers at lists.wordpress.org
> http://lists.wordpress.org/mailman/listinfo/theme-reviewers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.wordpress.org/pipermail/theme-reviewers/attachments/20130123/353ca3a7/attachment.htm>


More information about the theme-reviewers mailing list