Make WordPress Core

Opened 15 years ago

Closed 15 years ago

#8884 closed defect (bug) (fixed)

Bug In wp_enqueue_script() In Footer

Reported by: gamerz's profile GamerZ Owned by: azaozz's profile azaozz
Milestone: 2.8 Priority: high
Severity: normal Version: 2.8
Component: JavaScript Keywords:
Focuses: Cc:


I saw Andrew Ozz committing the patch to allow plugin authors to plugin their JS into the footer using wp_enqueue_script(). The script works in WP-Admin but when in the client side view, it works a little weird.

add_action('admin_print_scripts-wp-polls/polls-manager.php', 'poll_javascripts_admin');
function poll_javascripts_admin() {
        wp_enqueue_script('wp-polls-admin', plugins_url('wp-polls/polls-admin-js.js'), array('jquery'), '2.50', true);

The above code works perfect, with the last argument of wp_enqueue_script() set to true means print in the footer.

Now lets move onto the client side.

add_action('wp_print_scripts', 'poll_javascripts');
function poll_javascripts() {   
        wp_enqueue_script('wp-polls', plugins_url('wp-polls/polls-js.js'), array('jquery'), '2.50', true);

The above code is similar to that of the WP-Admin side with the last argument of wp_enqueue_script() set to true means print in the footer.

There is 2 problem with the code above.

  1. It will print in the footer of WP-Admin as well even though the hook is for "wp_print_scripts" and not "admin_print_scripts".
  1. In the client side, instead of printing it in the footer, it prints in the header.

I am using the default theme and hence there is a <?php wp_footer(); ?> in footer.php

Change History (4)

#1 @GamerZ
15 years ago

  1. I saw in default-filters.php that it "wp_print_scripts" is hooked onto "admin_print_scripts" (ignore that)
  2. Should we add in "add_action('wp_footer', 'wp_print_footer_scripts', 20);" as well?

#2 @azaozz
15 years ago

  1. "wp_print_scripts" is the "global" hook for adding scripts, it leaves to the plugin author to decide when to print the script. It is being deprecated by do_action('admin_enqueue_scripts', $hook_suffix); that runs in the admin head only, perhaps we can add "wp_enqueue_scripts" that would run in the client side only.

A lot of plugins seem to use "wp_print_scripts" for both the admin and client sides, so it will take time for this to be adopted.

  1. Yes, that seems to be the logical solution for adding footer scripts to the client side. Unfortunately a lot of themes don't call wp_footer(). We can encourage that, even ask the users to paste <?php wp_footer(); ?> just above the </body> tag in their theme. However some plugins use this hook to output HTML and the users may not expect that.

#3 @GamerZ
15 years ago

  1. Yea! I think we should add wp_enqueue_scripts" that would run on client side only. I am still back to the old method or hooking onto "wp_footer" using register/print scripts. I personally like the enqueue script function as I just need 1 function to print out the JS script.
  1. Hmmm, but if WP2.8 is focus on optimizing JS, I think we should have it called in the wp_footer as well as. Because no matter what, if the plugin's Javascript wants to be printed in the footer, it seems that wp_footer() is the only choice.

#4 @azaozz
15 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [10442]) Improvements to the script loader: allows plugins to queue scripts for the front end head and footer, adds hooks for server side caching of compressed scripts, adds support for ENFORCE_GZIP constant (deflate is used by default since it's faster), see #8628, fixes #8884

Note: See TracTickets for help on using tickets.