Chapter 6. WordPress and jQuery's UI

We're now ready to take a look at jQuery's most popular plugin: UI. UI of course, stands for User Interface. The jQuery UI plugin takes many of the most popular tasks that the developers have already made simple through jQuery, and makes them even simpler. I know, it's hard to imagine it getting any easier, but that's exactly what this plugin does. Most importantly, while the enhanced effects are nice, the UI plugin provides interface widgets and an easy way to style or "theme" them without the need for coding up specific interface elements such as tabs, dialog boxes, and more.

In this chapter, we'll:

Let's get started.

You can take a tour of the jQuery UI plugin by heading on over to http://www.jqueryui.com.

Getting to know jQuery's UI plugin

The UI plugin offers a set of standardized widgets, interactions, and effects. Let's take a look at each type of offering in detail.

The term "widget" within jQuery is a bit different from a WordPress widget, which is a small plugin designed to sit nicely in a sidebar of a theme. Within jQuery's UI plugin, widgets describe a set of fully-featured, user interface controls that are commonly needed in projects and created by jQuery developers. The UI widgets save jQuery developers a lot of time writing jQuery statements and chaining functions together to create the same interface and effect. Here are the interface widget's jQuery UI offers:

The main feature is the .effect() function, but the standard animation functions and shortcuts that are available in jQuery are enhanced with the jQuery UI plugin's "effects core". This core also includes the ability to color, animate, and also include additional easing options; so, if you include it into your project, you won't need the Color or Easing plugins that we've been working with previously. The jQuery effects comprise:

The advantage of downloading from the jQuery's UI site is you can pick and choose only what you need for your project. If you go to the download page at http://www.jqueryui.com/download, you'll see on the right-hand side that you can pick version 1.7.3 or 1.8.4 and click on the Download button; this will give you everything.

Picking and choosing from the jQuery's UI site

For development purposes, you can just download the whole thing. The ZIP file is over 4 MB but that includes a development bundle directory chock full of examples and documentation; none of that would get loaded into your project.

With all options selected, the actual UI plugin's .js file you'll load into your WordPress project is about 200 KB, and you can count on adding about another 100 KB to the project for your CSS theme depending on what you choose from the site or how you rolled your own. If you know exactly what features you're using, you can shave off some kilobytes by only selecting what you want to use.

The download page is great because it won't let you deselect anything that is dependent on another feature you've selected, and that you'd like to use. This is a screenshot of an alert for selecting something you need:

Picking and choosing from the jQuery's UI site

No matter where you're pulling in the UI plugin from, your own download, the Google CDN, or the WordPress bundled UI options, you'll need to provide your own styles for it. You can include one of many great themes into your project or easily "roll" your own to best match your site's design.

On the jQuery's UI site select Themes from the navigation bar, or go to: http://jqueryui.com/themeroller/.

Making it look right: Easy UI theming

You can tweak the resulting theme's CSS stylesheet directly or by simply loading the jQuery UI stylesheet up before your WordPress stylesheet. Using the WebDeveloper's Toolbar or Firebug in Firefox, it's very easy to see what styles the UI is producing and overwrite them in your main WordPress stylesheet.

By now, you should be pretty comfortable including jQuery plugins into your WordPress sites. Because specific components of the UI plugins are available bundled in WordPress, we'll review getting them into your project in a few different ways.

No matter where you're pulling the UI plugin from, WordPress, Google's CDN, or your own download, you'll need to include CSS styles for the UI plugin. If you didn't play around with the theme roller earlier, go back now and do so. Select a theme or amend one of the themes with the theme roller or just roll your own from scratch to create widgets that look great with your site's existing design.

Once you've done that, you can take your selected theme or custom rolled theme and place it in your theme or a plugin directory. Make sure to include the images directory that comes with the theme. You can then include it using a direct link into your header.php theme file or use the wp_enque_style function we've used before to include it into a plugin or your theme through the functions.php page:

To include a UI theme directly in your WordPress theme, by linking to it directly, use the following:

Include a UI theme into a WordPress theme from the theme's functions.php page using wp_enqueue_style:

Including a UI theme into a WordPress plugin using wp_enqueue_style, is similar to the above example, but be sure to use WP_PLUGIN_DIR to target your plugin directory.