WPbasics - A beginners resource for WordPress

A beginners resource for WordPress

  • Home
  • Blog
  • About
  • Contact Us
  • Start a Blog
  • Recommended Hosts

How to add a jQuery UI Accordion to WordPress

Last updated on February 22, 2016 By WPbasics 5 Comments

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.

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<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

JavaScript
1
2
3
$(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.

JavaScript
1
2
3
4
5
6
//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.

XHTML
1
2
3
<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

PHP
1
2
3
4
5
6
7
8
9
10
11
12
// 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
PHP
1
2
3
//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 like the one below.

Section 1

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.

Section 2

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.

Section 3

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.

  • List item one
  • List item two
  • List item three

Section 4

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.Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
inceptos himenaeos.

“DISCLOSURE: I may be an affiliate for products that I recommend. If you purchase those items through my links I will earn a commission. You will not pay more when buying a product through my link. In fact, I oftentimes am able to negotiate a lower rate (or bonuses) not available elsewhere. Plus, when you order through my link, it helps me to continue to offer you lots of free stuff. 🙂 Thank you, in advance for your support”

Share is caring:

  • Facebook
  • Twitter
  • Reddit
  • Pinterest
  • More
  • LinkedIn
  • Pocket
  • Tumblr
  • WhatsApp
  • Skype
  • Print
  • Email

Related

Filed Under: Accordion, jQuery UI, Tips and Tricks, Tutorials, Video Tutorials Tagged With: jQuery

Comments

  1. Qoder says

    December 26, 2017 at 9:20 pm

    Cool thnx, this helped me a lot 🙂

    Reply
  2. bibbli says

    February 4, 2018 at 2:27 am

    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?

    Reply
  3. Capucine Gaudry says

    May 31, 2018 at 10:07 am

    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?

    Reply
  4. Josef says

    July 13, 2018 at 1:45 pm

    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

    Reply
  5. Georgy says

    July 19, 2018 at 9:47 am

    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.

    Reply

Leave A Reply Cancel reply

Flywheel

Get Local by Flywheel

Recent Posts

  • ACF 5.8 – Introducing ACF Blocks for Gutenberg
  • How to disable the cache in Chrome while developing
  • What is your favourite browser for development
  • How to copy a live WordPress site to a localhost
  • How to Create a Localhost for a WordPress Site Using Local by Flywheel
  • Full width search form on top of the header
  • How to add a Utility Bar to the Altitude Pro Theme
  • Add a Search Icon to Mobile in Monochrome Pro
  • HTML and CSS Tutorials for Beginners
  • Enable Multiple Undos in Photoshop

Recommended Host

Web Hosting

Popular Posts

  • How to copy a live WordPress site to a local host | Duplicator
  • How to add a jQuery UI Accordion to WordPress
  • Advanced Custom Fields | Custom Front Page
  • From Bootstrap to WordPress
  • Replace the image background with a video | Altitude Pro
  • Equal Height Columns No Plugin
  • How to add jQuery UI Tabs to WordPress
  • Full width search form on top of the header
  • How to copy a live WordPress site to a localhost
  • How to Create a Localhost for a WordPress Site Using Local by Flywheel

Copyright © 2019 · WPbasics ·

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.