Adding jQuery UI tabs to WordPress with the enqueue function
In this tutorial I will teach you how to add jQuery UI tabs 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.
Copy and paste this code into your page/post.
<div id="tabs"> <ul> <li><a href="#tabs-1">Nunc tincidunt</a></li> <li><a href="#tabs-2">Proin dolor</a></li> <li><a href="#tabs-3">Aenean lacinia</a></li> </ul> <div id="tabs-1"> <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p> </div> <div id="tabs-2"> <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p> </div> <div id="tabs-3"> <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p> <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p> </div> </div>
Step 2: Add the jQuery tabs 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 the jQuery UI page from this
$(function() { $( "#tabs" ).tabs(); });
to this.
//jquery-ui-tabs jQuery(document).ready(function($) { $('#tabs').tabs(); });
- Create a folder called js inside your theme folder (if one does not already exist).
- Create a file called tabs.js inside the js folder.
- Paste the code above into the tabs.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 tabs script function my_scripts_tabs() { if ( !is_admin() ) { wp_enqueue_script('jquery-ui-tabs'); wp_enqueue_script( 'custom-tabs', get_stylesheet_directory_uri() . '/js/tabs.js', array('jquery')); } } add_action('wp_enqueue_scripts', 'my_scripts_tabs');
Step 4: Style your tabs
The last step is to add some styling to your tabs. 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 tabs 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 working tabs.
thank you very much! very well explained
thanks. great tutorial. Please assist with vertical tabs. Is there a Theme for vertical tabs?
I was getting following error after doing steps mentioned above.
Notice: wp_register_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.)
Did this small change based on “https://wordpress.stackexchange.com/questions/137104/wp-enqueue-script-was-called-incorrectly”
// enqueue tabs script
function my_scripts_tabs() {
if ( !is_admin() ) {
//add tabs stylesheet (Step#4 code)
wp_register_style(‘jquery-custom-style’, get_stylesheet_directory_uri().’/css/jquery-ui-themes-1.12.1/jquery-ui.css’, array(), ‘1’, ‘scr wp_enqueue_style(‘jquery-custom-style’);
wp_enqueue_script(‘jquery-ui-tabs’);
wp_enqueue_script( ‘custom-tabs’, get_stylesheet_directory_uri() . ‘/js/tabs.js’, array(‘jquery’));
}
}
add_action(‘wp_enqueue_scripts’, ‘my_scripts_tabs’);
Basically move the contents of step#4 inside the if condition of step#3 before calling js code. The issue mentioned above went away.
hi, since I want to use menu list for dropdown of tables for years (every table is for each year and has it’s own shortcode, for example [table id=1 /] , can you pls send me tutorial for it?
I believe correct widget for use is selectmenu – https://jqueryui.com/selectmenu/
what is code for .js file?
what is code for functions.php?
pls check and let me know.
p.s. pls step by step, because I am new in jQuery.
hi, i have some issues doing this on my site, i lose the format when i try to use the code, the library i use on this site is one ver up than the u use on the video, what can be the solution in this case?
library ver. is jquery 1.12.4