Make WordPress Core

Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#11923 closed enhancement (worksforme)

Allow supressing the hook in wp_print_(script|style)()

Reported by: scribu's profile scribu Owned by: azaozz's profile azaozz
Milestone: Priority: normal
Severity: normal Version:
Component: JavaScript Keywords: has-patch commit
Focuses: Cc:

Description

Here's an example of what I'm currently doing:

class My_Shortcode {
	static $add_script;
 
	function init() {
		add_shortcode('myshortcode', array(__CLASS__, 'handle_shortcode'));
		add_action('wp_footer', array(__CLASS__, 'add_script'));
	}
 
	function handle_shortcode($atts) {
		self::$add_script = true;
 
		// actual shortcode handling here
	}
 
	function add_script() {
		global $wp_scripts;
 
		if ( ! self::$add_script )
			return;
 
		wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
 
		$wp_scripts->do_items('my-script');
	}
}
 
My_Shortcode::init();

From here.

It would be nice to replace

$wp_scripts->do_items('my-script');

with

wp_print_scripts('my-script');

The problem is that wp_print_scripts() fires the 'wp_print_scripts' hook, which can cause problems, like duplicate JavaScript code, etc.

All that's needed is a way to suppress that hook, when needed.

Attachments (1)

11923.diff (1.2 KB) - added by scribu 15 years ago.
second argument for wp_print_scripts() and wp_print_styles()

Download all attachments as: .zip

Change History (7)

@scribu
15 years ago

second argument for wp_print_scripts() and wp_print_styles()

#1 @scribu
15 years ago

  • Keywords commit added

#2 @Denis-de-Bernardy
15 years ago

won't this prevent depends from being fetched?

#3 @scribu
15 years ago

I use the method in one of my plugins and it works great.

I would just like to not have to call $wp_scripts myself.

Also, the patch is 100% backwards-compatible with everything. :)

#4 follow-up: @archon810
15 years ago

Just to clarify, the duplicate javascript code would be the result of the queue getting flushed and then potentially filled again with the same scripts, right (as far as I remember, the queue is emptied when it's printed)?

Wouldn't the ideal solution be to call a wp_print_scripts() again in the footer to flush out everything queued up for it? This way you don't have to manually call wp_print_scripts().

#5 in reply to: ↑ 4 @scribu
15 years ago

  • Milestone 3.0 deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Replying to archon810:

Just to clarify, the duplicate javascript code would be the result of the queue getting flushed and then potentially filled again with the same scripts, right (as far as I remember, the queue is emptied when it's printed)?

Actually, no. You can call wp_print_scripts() as many times as you want, and you won't get duplicate scripts.

I was thinking of plugins doing other stuff, like manually outputing <script> tags, but that would be foolish.

So I guess there's nothing to worry about.

Wouldn't the ideal solution be to call a wp_print_scripts() again in the footer to flush out everything queued up for it? This way you don't have to manually call wp_print_scripts().

Yes, that would be the ideal solution. I'll open a new ticket for that.

#6 @scribu
15 years ago

And here it is: #11944

Note: See TracTickets for help on using tickets.