Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was wondering if we can set the order of a function hooked to wp_footer(), I created a function that echos:

<script> ajsfunction(); </script>

and I also enqueued the js file of this function and printed it in the footer but the above is being printed before linking to the js file so it's not working, I'm doing this because this script is not run on all pages only on specific ones.

if we can't do this with wp_footer is there a way to output this script tag after the enqueued js file ?

thanks in advance.

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

The recommended way is to call your function inside the .js file you are enqueuing. Is there any reason you can't do that?

If you need access to dynamic data like WP options from within your script, use wp_localize_script to initialize a javascript object with the data you need:

$data = array('some_option' => get_option('an_option_from_my_theme'));

wp_enqueue_script('your_script_handle', 'path/to/the/script.js');
wp_localize_script('your_script_handle', 'my_theme_options', $data);

In your script you can access the data like you would a javascript object:

console.log(my_theme_options.an_option_from_my_theme);


Following your last comment, you have two possibilities:

  1. Put your code inside another javascript file that you enqueue, and make the plugin a dependency of this file.

  2. Add your code within the wp_footer hook, after WP prints footer scripts:

    add_action('wp_footer', function(){
    
     ?>
     <script>
    
     // your javascript
    
     </script>
     <?php 
    
     }, 100);
    

    "100" is the priority, the higher the number - the lower the execution priority of your action. Scripts are printed with the "20" priority

I'd go with (1) if you have a lot of js code, otherwise inline javascript is fine (2).

share|improve this answer
    
The js file is a jQuery plugin, I'm doing that to separate the options from the plugin and I don't want to add anything to the plugin file, and my function contains options for this plugin, that's why –  Peter Apr 7 '12 at 10:00
    
thank you this is really helpful. and one question what is my_theme_options in the wp_localize_script ? –  Peter Apr 7 '12 at 10:11
    
It's the name of the object that will get created. Do a little test, view the page source and you will understand easier what wp_localize_script does. –  onetrickpony Apr 7 '12 at 10:16
add comment

Look at this..

http://codex.wordpress.org/Function_Reference/add_action

You can set priority over two hooked functions

add_action('wp_footer','your_func',100);
add_action('wp_footer','your_func1',200);
share|improve this answer
    
thank you, and the default priority is 10 and like @One Trick Pony said above that scripts are printed with 20 priority I'll set it to higher than 20. –  Peter Apr 7 '12 at 10:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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