How to add jQuery UI Accordion to WordPress
In this tutorial I will teach you how to add a jQuery UI accordion to WordPress. There are a number of ways to do it. But when adding jQuery to WordPress it is best to follow the method outlined in the WordPress Codex under Function Reference/wp enqueue script . This is the method I will teach you today.
Step 1: Add the HTML markup
The first thing that we need to do is to add the HTML markup to our page or post. Be sure to add the HTML markup in the text editor (text tab) and not your visual editor. For this example I copied the HTML code from the jQuery UI website. The way that this jQuery UI accordion works is that the text inside the h3 header (i.e <h3>Section 1</h3>) becomes the title and the text inside the adjacent <div> becomes the content of the each individual accordion section.
Copy and paste this code into your page/post.
<div id="accordion"> <h3>Section 1</h3> <div> <p> Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate. </p> </div> <h3>Section 2</h3> <div> <p> Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna. </p> </div> <h3>Section 3</h3> <div> <p> Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui. </p> </div> <h3>Section 4</h3> <div> <p> Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est. </p> </div> </div>
Step 2: Add the jQuery UI accordion code
The jQuery library included with WordPress is set to the noConflict() mode. This is to prevent compatibility problems with other JavaScript libraries that WordPress can link to.
In the noConflict() mode, the global $ shortcut for jQuery is not available, therefore we have to modify the code provided by jQuery UI page from this
$(function() { $( "#accordion" ).accordion(); });
to this. I have modified the code so that the accordion is completely collapsed and so that the accordion height is set to the height of the content. Line 4 can be commented out if you do not like my extra modifications.
//jquery-ui-accordion jQuery(document).ready(function($) { $( "#accordion" ).accordion({ collapsible: true, active: false, heightStyle: "content" }); });
- Create a folder called js inside your theme folder (if one does not already exist).
- Create a file called accordion.js inside the js folder.
- Paste the code above into the accordion.js file.
Step 3: Enqueue your script
When working with “regular” websites you would reference your jQuery script by adding something like the code below between the <head> </head> tags of your HTML.
<head> <script src="jquery-1.11.3.min.js"></script> </head>
However, this is not the WordPress way. Although it is possible to add code directly to the head, you are just asking for problems due to potential conflicts with other scripts.
The WordPress way is to Enqueue the script through the functions.php file.
Copy and paste the code below into your functions.php file
// enqueue script function my_scripts_method() { if ( !is_admin() ) { wp_enqueue_script('jquery-ui-accordion'); wp_enqueue_script( 'custom-accordion', get_stylesheet_directory_uri() . '/js/accordion.js', array('jquery') ); } } add_action('wp_enqueue_scripts', 'my_scripts_method');
Step 4: Style your jQuery UI accordion
The last step is to add some styling to your jQuery UI accordion. The easiest way to add the styling is to visit the jQuiry UI ThemeRoller page and to select a theme you like from the Gallery section. You can even customize the themes. Once you have selected or customized a theme to your liking download the file to a place that you will remember. I usually download it to the desktop. I like to keep my jQuery UI CSS styles separate so I recommend you follow the steps below;
- Create a folder called CSS (if you don’t already have one) inside your themes folder.
- Find the file you downloaded from the jQuery UI ThemeRoller page and unzip the contents.
- copy the folder called jquery-ui-1.11.4.custom and past it into your CSS folder.
- Add the code below to your functions.php file
//add accordion stylesheet wp_register_style('jquery-custom-style', get_stylesheet_directory_uri().'/css/jquery-ui-1.11.4.custom/jquery-ui.css', array(), '1', 'screen'); wp_enqueue_style('jquery-custom-style');
That’s all there is to it. You should now have a working jQuery UI accordion.
Cool thnx, this helped me a lot 🙂
This is great, thanks! I’ve applied a background image to a section, but need to have that background visibly stick out of the top a certain amount, so I’m only hiding the bottom 80% or so. Any ideas?
Thanks a lot for this tutorial, it was very helpful! Now I would like the accordions panels to be opne on desktop and closed on mobile… I tried this:
jQuery(document).ready(function($) {
if $( window ).width() >= 960 {
$( “.accordion” ).accordion({
collapsible: false, active: true, heightStyle: “content”
});
} else {
$( “.accordion” ).accordion({
collapsible: true, active: false, heightStyle: “content”
});
}
});
but it’s not working… Any idea how to do this?
Thanks for the nice tutorial. I have a correction:
In step 4, you should change:
“Create a folder called CSS (if you don’t already have one) inside your themes folder.”
by:
“Create a folder called CSS (if you don’t already have one) inside your theme folder.”.
At first I taught you were talking about:
“wp-content/themes”
But no, you were talking about:
“wp-content/themes/My_Theme”
Thanks
Best regards
Josef
Hi!
your Code works great!
But i have one problem. I use the Code in the category post loop! So at the first post everything works perfekt, but in all the next post there is no accordion function.
Can you help me with that.