Updated December 8.
There was an earlier post on JavaScript changes in 3.3, but a lot has changed, so here it is again (and updated).
If JavaScript or visual editing broke in your plugin, start here.
jQuery 1.7.1
This release had strong backwards compatibility over jQuery 1.6.1 (which was bundled in WordPress 3.2) but there is still a chance that plugin JavaScript has broken. We will always attempt to bundle the latest jQuery version with every major release of WordPress, so if you plan to use jQuery, you should follow that project as well.
jQuery UI 1.8.16
jQuery UI has been updated to the latest version, and all UI components are now included in core, including widgets and effects. This will make it a lot easier and simpler for plugins using UI components that are not used in core as they will be able to just enqueue whatever they need. (For reference, WordPress 3.2 included part of 1.8.12.)
The wp_editor() API
Since the last post there have been some bug fixes for wp_editor(). This is an updated API for both TinyMCE and Quicktags that outputs all parts of both editors in the same way as used on the Add / Edit Post screens. Plugins will be able to use the WordPress editor anywhere — including rendering the Visual/HTML tabs and the links to upload files and show the media library.
Example usage:
$content = 'The content.';
$editor_id = 'foo';
$args = array(); // Optional arguments.
wp_editor( $content, $editor_id, $args );
Yeah, that’s it (though of course, you need to save it as well). Of note, there’s one pretty big gotcha: If you use wp_editor() to render the visual editor in a meta box, you risk problems. TinyMCE does not support being detached/moved in the DOM, which would occur when a meta box is dragged. (I’d look into the edit_form_advanced hook to render additional editors.)
For more, there’s a nascent Codex page on the API.
QuickTags
Since the previous post there have been a few improvements for Quicktags (the HTML editor toolbar), including better loading of the default buttons and “safe” close_all_tags() functionality. The major issue here is that we updated a JavaScript “API” that was almost as old as WordPress, so maintaining compatibility has been difficult.
Quicktags was refactored to make it fully multi-instance compatible (#16695). I think it still needs a Codex page, and I’ll ask @azaozz to post a tutorial here on how to use the new methods, and what before/after looks like in terms of converstion.
wp_localize_script() and wp_add_script_before()
When switching it to json_encode(), we opened up the possibility for encoding errors, so some changes were made to make this more backwards compatible. If you have previously used wp_localize_script() to pass arbitrary data (rather than localized strings), consider switching to wp_add_script_before().
Edit, December 8: wp_add_script_before() was removed.
Plupload
The old SWFUpload has been replaced with Plupload. Nothing here has really changed since the previous post, other than strings and the presentation of the drop zone.
wp_enqueue_script() now works mid-page
Yes: wp_enqueue_script(), called mid-page, will now enqueue the script into the footer. This isn’t new from the previous update.
Jared 4:58 pm on December 12, 2011 Permalink |
Whoops, I’ve been using wp_print_styles – thanks for the heads up!
Austin Passy 5:10 pm on December 12, 2011 Permalink |
Will start the update to my plugins. Especially the
login_enqueue_scripts
that should come in handy for my login plugin.Jamàl 5:18 pm on December 12, 2011 Permalink |
Will this effect
wp_register_style();
as well?Andrew Nacin 5:35 pm on December 12, 2011 Permalink |
Registering should also occur on _enqueue_scripts.
Banago 5:52 pm on December 12, 2011 Permalink |
wp_enqueue_script used to put scripts i.e. JavaScript in the backend too. It it this behavior that has been changed?
Joachim Kudish 6:44 pm on December 12, 2011 Permalink |
Just to make sure, this is backwards compatible (at least for 3.2) as well?
Andrew Nacin 8:21 pm on December 12, 2011 Permalink |
Yes, 100% backwards compatible. login_enqueue_scripts was added in 3.1, but wp_enqueue_scripts and admin_enqueue_scripts were both added much earlier (2.8, I think).
Joachim Kudish 8:29 pm on December 12, 2011 Permalink
Right on.
camu 7:45 pm on December 12, 2011 Permalink |
So, in other words, this article by Scribu is not valid anymore? http://scribu.net/wordpress/optimal-script-loading.html (linked from the official WP documentation, http://codex.wordpress.org/Function_Reference/wp_enqueue_script ) I’m asking because I need the registration and the “printing” to happen separately from each other. Unless there’s some other way to enqueue scripts in the footer. I’ve tried to use the last param in wp_enqueue_scripts, but if the template doesn’t call wp_head(), it fails… Separating registration and printing is the only way to solve this issue, afaik.
Camu
Andrew Nacin 8:30 pm on December 12, 2011 Permalink |
That article is fine, and doesn’t have to do with the issue here. (You’ll note that the
wp_print_styles
hook is not used.) However, WordPress 3.3 allowswp_enqueue_script()
to work mid-page, which means most of that Jedi logic is no longer needed.Camu 8:41 pm on December 12, 2011 Permalink
Thank you for clarifying
kwight 7:58 pm on December 12, 2011 Permalink |
How does wp_enqueue_style fit in to the mix (which I believe loads on both the front- and back-end)? I’ve just been wrapping it in an is_admin conditional up to this point…
kwight 7:59 pm on December 12, 2011 Permalink |
Oops, just noticed the wp_register_style comment above – clears that up then.
Andrew Nacin 8:29 pm on December 12, 2011 Permalink |
We’re only referring to the hooks
wp_print_styles
andwp_enqueue_scripts
. The actual functions,wp_enqueue_style()
orwp_enqueue_scripts()
should be called from hooks, not directly in the plugin body. (Which is what the post is about.)Ken Newman 8:12 pm on December 12, 2011 Permalink |
What about scripts/styles only for particular settings pages: any gotchas? (Is “admin_print_styles-$hook_suffix” still good or is there something better?)
Andrew Nacin 8:27 pm on December 12, 2011 Permalink |
While admin_print_styles-$hook_suffix is not as bad as wp_print_styles, note that the admin_enqueue_scripts hook does pass the $hook_suffix as the first parameter:
Kenneth Newman 10:08 pm on December 12, 2011 Permalink
Very good then. I see lots of source do admin_print_styles… It’s in core, twentyeleven, and popular plugins (so it can’t be terrible) but I’ll stick with the better new hotness tho in future
Kenneth Newman 10:17 pm on December 12, 2011 Permalink
You’re post about get_current_screen() as an alternative to $hook_suffix is helpful related to this http://wpdevel.wordpress.com/2011/12/06/help-and-screen-api-changes-in-3-3/
redwall_hp 2:36 am on December 13, 2011 Permalink |
That’s good to know. I don’t think I have any hooked on print_styles, but I think I might have on init on a few sites (with an if !is_admin() inside the function). That’s probably not great either, but at least it’s not going to blow up right away.