WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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:


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(){
     // your javascript
     }, 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 – Pierre 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 ? – Pierre 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

Look at this..


You can set priority over two hooked functions

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. – Pierre Apr 7 '12 at 10:17

Your Answer


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.