Add a jQuery UI Accordion Widget Area to a Wordpress Theme

Yahoo describes the accordion as a design pattern “that provides access to a large number of links or other selectable items in a constrained space.” If you are displaying a large amount of information and want to group them into logical sections, the accordion design pattern can be quite useful. This pattern has even more application in mobile web applications.

Today, we’ll look at how to create a jQuery UI-enabled accordion widget area for the popular Wordpress CMS.

Include jQuery and jQuery UI

There are a bunch of articles describing the proper way to include jQuery in a Wordpress theme by using wp_enqueue_script(). By adding a few lines to the theme’s functions.php file, the JavaScript files are referenced and loaded in the wp_head() portion of the Wordpress theme.

If you’d rather load the JavaScript files in the footer for performance reasons, you can also use a parameter in the wp_enqueue_script() function which will load the scripts in the wp_footer() function as described in the codex.

Create the Sidebar Structure

In order to use jQuery UI’s accordion, the structure that the markup needs is very specific.

<div id="accordion">
    <h3><a href="#">First header</a></h3>
    <div>First content</div>
    <h3><a href="#">Second header</a></h3>
    <div>Second content</div>
</div>

The important thing to note is that Wordpress wraps all widgets in a <div> tag, so you are halfway there already. The <h3> and <a> tags can be added in the functions.php file which will take care of the rest of the structure:

Load the Sidebar and Call it in the Wordpress Theme

Next, we want to load the widget area to a sidebar file such as sidebar.php.

Then load the sidebar in the page template. The accordion is then displayed on the front-end of the WP theme by adding <?php get_sidebar('accordion-widgets'); ?> in the appropriate file(s). Usually, this will be added to page.php and/or index.php.

Final Steps

The last steps are to instantiate the accordion in the theme JavaScript file and then reference the script file and the jQuery UI CSS skin in the theme’s header.php file with <script> and <link> tags respectively. The <script> tag should go after the wp_head() function to make sure all necessary dependencies are loaded before the accordion is invoked. After that, you should have a functional accordion widget area. The only caveat with this technique is that all the widgets need to have a title in the Wordpress backend, otherwise the markup required by jQuery UI’s accordion won’t be met.

jQuery UI Accordion in Wordpress

Conclusion

With a few minutes of work, this useful feature can be added to any Wordpress theme. To see everything in action, you can download the example Wordpress child theme from Github. The child theme is based on the TwentyEleven theme that ships with WP3.2, so it is probably best to download the file and activate it on a local install for development or testing purposes.

  • Filed under Development
  • By Ethan Gardner
  • Posted on 12th Oct 2011
  • Comments (3)

Like what you're reading?

If you find the topics I write about interesting or helpful, please consider subscribing or follow me on twitter.

I can also provide services similar to the topics I write about if you'd like to get in touch.

Contact me today

Recent Articles

Comments

LPNtoRN Bridge Programs13 Oct 11, 1:12am

So I am close but can't get it to work. I think there should be a slight revision on a couple things. One, you provide wpenqueuescript registers for the functions page and then state that these codes must go after the wphead() tag. I was confused over this because I thought the wphead() tag did all the enqueue requests in it. Just a little unclear.

I also had to hunt for the .css tag for the jquery-ui didn't know where to put that exactly so I left it in wp_head() where it seems to be getting loaded.

Nonetheless, I can't get it to work. I have also added one more difficulty to the mix, my included code that should appear in my widgets is actually the output of a function in functions.php so I just modified the output of the function to have the wrapping accordian div, a pound anchor in the h3 tags and a blank div around the inner content.

I was thinking that having an h3 tag was equivalent to having a widget title?

I will try a couple variations probably before you get back to me on this comment, but thanks a ton for posting!

LC

Ethan Gardner14 Oct 11, 12:42am

@LC - Glad you enjoyed the post. For clarification, the jQuery and jQuery UI files are included inside of the wp_head() by the enqueue script functions. The JS file that invokes the accordion needs to go after those. What I mean is that you usually have a separate file that actually has all of the code that provides the site functionality. It might be more clear if you take a look at the sample files. The file I'm referring to is called ux.js and it is called in the header.php file on line 73 of my sample.

The jQuery UI CSS skin is being called on line 51 of the header.php file in the sample files.

You are correct in that the <h3> tag would be the equivalent of having a widget title in the widget area I created for this post so as long as you are adhering to the markup that jQuery UI's accordion is expecting, everything else should work.

LPNtoRN Bridge Programs14 Oct 11, 2:45pm

Ok I have returned to say I finally got it with a little hem-hawing around. It was not apparent to me that there were a selection of files that needed to be included.

I downloaded the example you provided on Github and stripped those out and whala it works.

Now to customize the css. Thank you for posting this article man no other off the shelf plugins are working :)

LC

Make a comment

Rules

  • Text is formatted with Markdown.
  • Abusive or hateful comments will be removed.