<div dir="ltr"><div><div><div><div>Thanks Otto,<br></div>That's exactly what I was looking for.<br></div>In fact, I am currently looking at your post here<br><a href="http://ottopress.com/2010/passing-parameters-from-php-to-javascripts-in-plugins/">http://ottopress.com/2010/passing-parameters-from-php-to-javascripts-in-plugins/</a><br>
<br></div>Chip,<br></div>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.<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Wed, Jan 23, 2013 at 12:15 AM, Otto <span dir="ltr"><<a href="mailto:otto@ottodestruct.com" target="_blank">otto@ottodestruct.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
That is probably not the right way. The "right" way depends on the<br>
contents of your script code there, actually.<br>
<br>
<br>
If you're needing script dependencies, where you have to load one<br>
external script before another, then wp_register_script and<br>
wp_enqueue_script support this themselves. Just register all the .js<br>
links and use the $deps argument to make them dependent on the proper<br>
scripts. Like jquery-ui is dependent on jquery, for example. Then the<br>
script printer will put them all in the right order.<br>
<br>
<br>
If you need to add inline variables for scripts to use, then<br>
wp_localize_script will do that. For example, I can register a script<br>
and pass PHP variables to it as named parameters, like so:<br>
<br>
wp_register_script('example', '<a href="http://example/whatever.js'" target="_blank">http://example/whatever.js'</a>);<br>
<br>
wp_localize_script('example', 'Example', array(<br>
'examplea' => 'foo',<br>
'exampleb' => 'bar',<br>
) );<br>
<br>
When I then enqueue the 'example' script later, the script printer<br>
will recognize the localization as a dependency, and print this out in<br>
a script tag:<br>
<br>
var Example = {"examplea":"foo","exampleb":"bar"}<br>
<br>
The external .js script can then access these variables as<br>
Example.examplea and Example.exampleb.<br>
<br>
wp_localize_script() was meant for allowing the Internationalization<br>
code to be able to pass translated strings into the javascript files,<br>
but it doesn't have to be limited to that. You can pass any variable<br>
data you can put into the array in this manner.<br>
<span class="HOEnZb"><font color="#888888"><br>
-Otto<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
<br>
On Tue, Jan 22, 2013 at 12:34 PM, Abhik Biswas <<a href="mailto:abhik@itsabhik.com">abhik@itsabhik.com</a>> wrote:<br>
> Okay,<br>
> So, what if I need to put something *before* my enqueued scripts, what<br>
> should I use?<br>
><br>
> Is this the right way?<br>
><br>
> function my_scripts() { ?><br>
> <script type="text/javascript"><br>
> ........<br>
> </scripts><br>
><br>
> <?php }<br>
> add_action ('wp_enqueue_scripts', 'my_scripts', 5 );<br>
><br>
><br>
><br>
><br>
><br>
> function my_enqueued_scripts() {<br>
><br>
> wp_enqueue_script ('myscript', ...... myotherscript.js....., '');<br>
><br>
> }<br>
> add_action ('wp_enqueue_scripts', 'my_enqueued_script', );<br>
><br>
><br>
> On Tue, Jan 22, 2013 at 11:56 PM, Otto <<a href="mailto:otto@ottodestruct.com">otto@ottodestruct.com</a>> wrote:<br>
>><br>
>> The question itself is slightly confusing, because it kinda depends on<br>
>> the purpose...<br>
>><br>
>> For the front-end of the site, the wp_print_scripts action is actually<br>
>> executed from the wp_print_head_scripts() function.<br>
>><br>
>> The wp_print_head_scripts() function is actually hooked to the wp_head<br>
>> action, like so:<br>
>> add_action( 'wp_head', 'wp_print_head_scripts', 9 );<br>
>><br>
>> Same goes for wp_enqueue_scripts. The wp_enqueue_scripts action is in<br>
>> the wp_enqueue_scripts() function which is called like so:<br>
>> add_action( 'wp_head', 'wp_enqueue_scripts', 1 );<br>
>><br>
>> So the answer is different depending on whether you consider the<br>
>> action to have occurred when the action is called or when the action<br>
>> has returned.<br>
>><br>
>> The long and the short of it is that both enqueuing and printing of<br>
>> scripts happen inside the wp_head() call. So the question of ordering<br>
>> isn't something that external code really needs to consider. Instead,<br>
>> you need to know that you should add scripts on wp_enqueue_scripts,<br>
>> and remove scripts on wp_print_scripts (as this is the last possible<br>
>> moment to remove them before they get printed to the output).<br>
>><br>
>> -Otto<br>
>><br>
>><br>
>> On Tue, Jan 22, 2013 at 12:16 PM, Michael Fields <<a href="mailto:michael@mfields.org">michael@mfields.org</a>><br>
>> wrote:<br>
>> > Here's a simple way to test this:<br>
>> ><br>
>> > <?php<br>
>> > function mfields_test_some_hooks() {<br>
>> > static $i = 1;<br>
>> > var_dump( $i . ' - ' . current_filter() );<br>
>> ><br>
>> > $i++;<br>
>> > }<br>
>> ><br>
>> > add_action( 'wp_head', 'mfields_test_some_hooks' );<br>
>> > add_action( 'wp_print_scripts', 'mfields_test_some_hooks' );<br>
>> > add_action( 'wp_enqueue_scripts', 'mfields_test_some_hooks' );<br>
>> > ?><br>
>> ><br>
>> > I get the following order:<br>
>> ><br>
>> > 1 - wp_enqueue_scripts<br>
>> > 2 - wp_print_scripts<br>
>> > 3 - wp_head<br>
>> > _______________________________________________<br>
>> > theme-reviewers mailing list<br>
>> > <a href="mailto:theme-reviewers@lists.wordpress.org">theme-reviewers@lists.wordpress.org</a><br>
>> > <a href="http://lists.wordpress.org/mailman/listinfo/theme-reviewers" target="_blank">http://lists.wordpress.org/mailman/listinfo/theme-reviewers</a><br>
>> _______________________________________________<br>
>> theme-reviewers mailing list<br>
>> <a href="mailto:theme-reviewers@lists.wordpress.org">theme-reviewers@lists.wordpress.org</a><br>
>> <a href="http://lists.wordpress.org/mailman/listinfo/theme-reviewers" target="_blank">http://lists.wordpress.org/mailman/listinfo/theme-reviewers</a><br>
><br>
><br>
><br>
> _______________________________________________<br>
> theme-reviewers mailing list<br>
> <a href="mailto:theme-reviewers@lists.wordpress.org">theme-reviewers@lists.wordpress.org</a><br>
> <a href="http://lists.wordpress.org/mailman/listinfo/theme-reviewers" target="_blank">http://lists.wordpress.org/mailman/listinfo/theme-reviewers</a><br>
><br>
_______________________________________________<br>
theme-reviewers mailing list<br>
<a href="mailto:theme-reviewers@lists.wordpress.org">theme-reviewers@lists.wordpress.org</a><br>
<a href="http://lists.wordpress.org/mailman/listinfo/theme-reviewers" target="_blank">http://lists.wordpress.org/mailman/listinfo/theme-reviewers</a><br>
</div></div></blockquote></div><br></div>