Make WordPress Core

Opened 16 years ago

Closed 13 years ago

Last modified 13 years ago

#9346 closed enhancement (fixed)

wp_enqueue_script() doesn't work mid-page for footer scripts

Reported by: viper007bond's profile Viper007Bond Owned by: azaozz's profile azaozz
Milestone: 3.3 Priority: lowest
Severity: minor Version: 2.8
Component: JavaScript Keywords: needs-docs needs-patch
Focuses: Cc:

Description

Create a footer script at init, plugins_loaded, or whatever. Doesn't matter, just early.

wp_register_script( 'myscript' '/some/path/script.js', array(), '1.2.3', true );

Now if we were to do wp_enqueue_script( 'myscript' ); directly after that, the script would be outputted in the footer as expected.

However if we were to do the enqueue farther down the page, say inside a the_content filter, it doesn't work. It should. I need to be able to dynamically enqueue scripts for example based on shortcodes.

Attachments (3)

9346.diff (523 bytes) - added by Denis-de-Bernardy 16 years ago.
print-scripts-footer.diff (867 bytes) - added by amattie 14 years ago.
Alternative solution for consideration purposes
9346-2.patch (5.2 KB) - added by azaozz 13 years ago.

Download all attachments as: .zip

Change History (39)

#1 @Viper007Bond
16 years ago

  • Summary changed from wp_enqueue_script() doesn't work mid-page to wp_enqueue_script() doesn't work mid-page for footer scripts

Missing comma inside that register:

wp_register_script( 'myscript', '/some/path/script.js', array(), '1.2.3', true );

And incase you didn't notice, the fifth parameter is set to true which makes it output in the footer.

#2 follow-up: @Viper007Bond
16 years ago

Debugged and found the problem.

When WP_Scripts::do_item() fires for outputting the header scripts and comes across a script that is meant for the footer, it tosses that that handle into the WP_Scripts::in_footer array and aborts outputting it.

Calling wp_enqueue_script() after that point doesn't add the script to the WP_Scripts::in_footer array, so it never gets properly registered.

I have a couple solutions in mind, but I'll let someone more at home with the class (azaozz perhaps?) come up with the solution to ensure it's the cleanest one.

#3 @sivel
16 years ago

  • Cc matt@… added

#4 follow-up: @azaozz
16 years ago

Yes the script loader requires all scripts to be queued before the page starts to load so it can do the dependencies properly. It also supports wp_print_script() in the middle of the page and would print all dependencies (if not already in the head) and remove any of them if queued for the footer.

I suppose we can add support for wp_enqueue_script() in the middle of the page that would queue scripts only for the footer. Would need to force all dependencies to print in the footer too even if queued for the head by default.

That may bring some issues with scripts that cannot run from the footer properly and are printed as dependency. Perhaps would be better to leave this as an "advanced" option only.

#5 @Viper007Bond
16 years ago

  • Priority changed from normal to lowest
  • Severity changed from normal to minor
  • Type changed from defect (bug) to enhancement

I didn't think of just using wp_print_scripts( 'myscript' ); inside a footer hook. That works nicely.

Dropping the priority on this and switching it to an enhancement as there's an alternate solution.

#6 in reply to: ↑ 2 ; follow-up: @hakre
16 years ago

Replying to Viper007Bond:

Debugged and found the problem.

When WP_Scripts::do_item() fires for outputting the header scripts and comes across a script that is meant for the footer, it tosses that that handle into the WP_Scripts::in_footer array and aborts outputting it.

Calling wp_enqueue_script() after that point doesn't add the script to the WP_Scripts::in_footer array, so it never gets properly registered.

I have a couple solutions in mind, but I'll let someone more at home with the class (azaozz perhaps?) come up with the solution to ensure it's the cleanest one.

thanks for getting into it. a propper dev feedback would be nice, because as you I think this should be easily solveable.

#7 in reply to: ↑ 6 @Denis-de-Bernardy
16 years ago

+1 for this one

#8 @Denis-de-Bernardy
16 years ago

  • Keywords has-patch tested added; needs-patch removed
  • Milestone changed from Future Release to 2.8

Attached patch does the trick. tested with a footer script that required recursive depends including a few that should go in the header.

Before the patch: nothing gets added at all.

After the patch:

  • all depends (including recursively needed depends) get added in the footer if none is previously added
  • already included depends do not get added as expected

Potential issues I can think of:

  • localized scripts (should work fine)
  • scripts that really should be in the header, but I think it is the plugin author's job to make sure his scripts work.

#9 @Denis-de-Bernardy
16 years ago

  • Keywords commit added

#10 @azaozz
16 years ago

  • Milestone 2.8 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Still think it's better to use wp_print_scripts() in the middle of the page or in the footer. The patch may behave unexpectedly when wp_print_scripts() is used after it but before the footer action.

#11 @aaroncampbell
15 years ago

  • Cc aaroncampbell added

I just thought I'd add in my two cents. First, this would be really handy to have for the exact reason that Viper mentioned, shortcodes. Shortcodes have become an extremely useful tool, but the ability to easily enqueue scripts would really put them over the top. Right now I'm using the suggested method of a wp_footer action that calls wp_print_scripts, but it's not as elegant as using wp_scripts.

Hopefully we can take a look at this again some day soon.

#12 @archon810
14 years ago

  • Cc admin@… added

+1 for a proper solution - the use case that Viper described (shortcodes) makes this look like a bug - the feature of printing scripts in the footer does not work as expected.

#13 in reply to: ↑ 4 @scribu
14 years ago

Can't wait for jQuery's require() method.

#14 @scribu
14 years ago

Related: #15126

#15 @rspindel
14 years ago

  • Cc rspindel added

#16 @amattie
14 years ago

  • Cc amattie@… added

I just ran into this issue when trying to add a script to the footer within a shortcode. I considered this to be bug and created a patch before looking for and finding this ticket. I'll be attaching that patch for debate and consideration purposes.

In order to address my needs for now, I will manually add my scripts to in_footer[] immediately after the enqueue.

@amattie
14 years ago

Alternative solution for consideration purposes

#17 @scribu
14 years ago

  • Keywords tested commit removed
  • Milestone set to Future Release
  • Resolution wontfix deleted
  • Status changed from closed to reopened

Re-opening for reconsideration, given amattie's patch.

#18 @azaozz
14 years ago

The second patch forces a script in the footer without accounting for the dependencies. When that script depends on another script that prints in the head by default (and is not already printed) it would break.

I would rather go with the first patch (by Denis). It prints all scripts that haven't been printed yet in the footer regardless of the default location.

#19 @scribu
14 years ago

Related: #15843

#21 @philipwalton
14 years ago

For what it's worth, I ran into another situation other than shortcodes where I needed to make the decision whether or not to enqueue a script after the admin_enqueue_scripts hook was fired. In this case on the admin side.

I'm using an MVC plugin framework that loads a view file for the settings form. In the view file the user of the framework creates a form object, e.g:

$form = new MVC_Active_Form;
$form->ajax_validation = true;

So the framework has no idea whether or not the settings form will require ajax validation until after WordPress loads it, which doesn't happen until well after the admin_enqueue_scripts hook is fired.

Anyway, I just thought I'd offer another example to support a better implementation of wp_enqueue_script() in core. In the meantime the proposed work-around is doing the trick.

#22 @mitchoyoshitaka
13 years ago

  • Cc mitcho@… added

Working on #17704 now and realized I can't do it neatly because of this bug. Just another reason why getting this fixed properly would be appreciated.

#23 @billerickson
13 years ago

  • Cc bill.erickson@… added

#24 @azaozz
13 years ago

  • Milestone changed from Future Release to 3.3

#25 @travisnorthcutt
13 years ago

  • Cc travis@… added

@azaozz
13 years ago

#26 @azaozz
13 years ago

9346-2.patch adds support for late enqueueing of both scripts and styles which are printed in the footer.

#27 @azaozz
13 years ago

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

In [18446]:

Support for using wp_enqueue_script() and wp_enqueue_style() in the HTML body. All scripts and styles are added in the footer, fixes #9346

#28 follow-up: @nacin
13 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Lots of new functions, should docs would be nice.

Would be good to get westi to look at this as well, as he's generally watched the dependencies closely. Some of the stuff in [18446] looks a bit cryptic.

#29 in reply to: ↑ 28 @azaozz
13 years ago

Replying to nacin:

The only "real" change is making in_array() in class.wp-dependencies.php do a strict comparison. The rest is copy/paste between the wp-scripts and wp-styles functions. It would be better to open new tickets for any further problems.

Last edited 13 years ago by azaozz (previous) (diff)

#30 @WraithKenny
13 years ago

  • Cc Ken@… added

#31 @ocean90
13 years ago

  • Keywords needs-docs needs-patch added; has-patch removed

#32 @azizur
13 years ago

  • Cc azizur added

#33 @ryan
13 years ago

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

#34 follow-up: @michalsrodek
13 years ago

  • Cc michalsrodek added
  • Type changed from enhancement to defect (bug)

Unfortunately it's not the best idea to add styles in the footer. In some cases websites are loading very long and some parts are unstyled until whole page is loaded. I think that it would be better to buffer everything after head section using ob functions(http://www.php.net/manual/en/ref.outcontrol.php).

#35 @ocean90
13 years ago

  • Type changed from defect (bug) to enhancement

If you have problems you should open a new ticket.

#36 in reply to: ↑ 34 @aaroncampbell
13 years ago

Replying to michalsrodek:

Unfortunately it's not the best idea to add styles in the footer

This thicket was for scripts not styles.

Note: See TracTickets for help on using tickets.