change wp_scripts->do_footer_items for usability better script enqueueing
-
Currently, if I have to enqueue script, I have two cases:
1) enqueue it in init (in theme or in plugin) with wp_enqueue_script. that’s not a good solution to me ‘couse the script might be not needed in all pages. too much garbage in page.
2) insert script right where it’s needed – that’s not a very bad case ‘couse mixing html page markup with scripts is BAD.
a good place for scripts is header and footer.
a good solution is to allow a user to enqueue a script in any place beforewp_scripts->do_footer_items
.
as for now,wp_scripts->in_footer
is calculated in header and user can’t enqueue scripts afterwp_scripts->do_head_items
.please change
wp_scripts->do_footer_items
.
this will allow to enqueue scripts anywhere before wp_scripts->do_footer_items.function do_footer_items() { $in_footer = array_diff($this->queue, $this->done); $this->all_deps($in_footer); if ( !empty($this->to_do) ) { foreach( $this->to_do as $key => $handle ) { if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) { $this->do_item($handle); $this->done[] = $handle; unset( $this->in_footer[$key] ); } } } return $this->done; }
there might be better way, but this works perfectly for me.
- The topic ‘change wp_scripts->do_footer_items for usability better script enqueueing’ is closed to new replies.