How to Add jQuery Tabber Widget in WordPress

How to Add a jQuery Tabber Widget in WordPress (2023 Guide)

Have you ever visited a website and seen a handy tabbed widget that lets you quickly view different categories of content all in one place? Many popular sites use these tabber widgets to display their latest posts, most popular articles, comments, and more. As a WordPress user, you may be wondering how you can add this useful feature to your own site.

In this expert guide, we‘ll walk you through exactly how to create a custom jQuery tabber widget and add it to your WordPress site. Don‘t worry if you‘re not a coding guru – we‘ll provide step-by-step instructions and example code that even beginners can follow. By the end, you‘ll have an professional, functional tabber widget that will engage visitors and enhance your WordPress site.

What is a jQuery Tabber Widget?

First, let‘s clarify what we mean by a "jQuery tabber widget." A tabber is a user interface element that allows you to organize and display content under multiple tabs in a compact space. When a user clicks a tab, the corresponding content is shown and the other tab content is hidden. jQuery is a popular Javascript library that makes it easy to create interactive features like tabbers.

Many WordPress sites employ tabber widgets in their sidebars to display different content categories without taking up too much precious space. Common uses for tabbers include:

  • Displaying latest posts, popular posts, and random posts
  • Showing latest comments or most commented posts
  • Presenting products or featured items in an online store
  • Organizing widgets into categories

The tabber consolidates multiple widgets or content sections into a single area, letting the user choose which to view. This keeps your WordPress site looking clean and uncluttered while still providing quick access to important content. Tabbers are especially handy for sites that have a lot of information to display in a sidebar or other widget areas.

Why Create a Custom jQuery Tabber Widget?

While there are existing WordPress plugins that enable you to create tabbed widgets, there are advantages to building your own custom solution:

  1. Full control over styling – Tailor the tabber‘s look and feel to perfectly match your WordPress theme. Choose fonts, colors, spacing and more.

  2. Unlimited content options – Include any content you want in the tabs, not just limited to posts, comments, etc. Display custom post types, RSS feeds, images, HTML, and anything else.

  3. No unnecessary bloat – Some WP tabber plugins include many extra features you may not need. Creating your own lean, focused plugin avoids weighing down your site.

  4. Valuable learning experience – For beginning to intermediate WordPress developers, coding your own widget is great practice with creating plugins, working with jQuery, and integrating with WordPress functionality.

Of course, building a tabber widget from scratch requires some web development knowledge, including familiarity with HTML, CSS, JavaScript/jQuery, and PHP. But don‘t let that scare you away – this guide will walk you through each step. And the experience will boost your WordPress skills enormously, empowering you to craft the exact features and functionality your site needs.

Roadmap to Building the Tabber Widget

Our jQuery tabber widget will be contained in a custom WordPress plugin. The plugin will be composed of a main PHP file registering the widget and enqueueing scripts/styles, plus separate CSS and JS files holding the widget styling and behaviors.

Here‘s a quick overview of the process we‘ll follow:

  1. Create a folder for the plugin files
  2. Create the main PHP file with the widget code
  3. Create a CSS file with the tabber styling
  4. Create a JS file with the jQuery tab switching code
  5. Activate the plugin and add the widget to a sidebar

The sample CSS and JS code is relatively basic to keep this guide beginner-friendly. You can customize and enhance the styling and behavior to your needs. The key concepts are creating the widget in PHP, properly linking the scripts/styles, and implementing the jQuery tab functionality.

Let‘s get started!

Step 1: Create the Plugin Folder and Files

The first step is to create a folder on your computer that will store all the plugin‘s files. Give it a descriptive name like my-tabber-widget. Inside this folder, create empty files with the following names:

  • my-tabber-widget.php
  • tabber-styles.css
  • tabber-script.js

We‘ll add the appropriate code to each of these files in the following steps. The PHP file will contain the main widget code and plugin information, the CSS file will style the widget, and the JS file will make the tabs interactive.

Step 2: Code the Main PHP Widget File

Open up the my-tabber-widget.php file in your preferred code editor or IDE. Add the following code:

<?php
/
Plugin Name: My Tabber Widget
Plugin URI: https://www.mysite.com/
Description: A custom jQuery tabber widget for WordPress
Version: 1.0
Author: Your Name
Author URI: https://www.mysite.com/
/

// Block direct access to the plugin PHP files
defined( ‘ABSPATH‘ ) or die( ‘No script kiddies please!‘ );

// Create the widget class
class My_Tabber_Widget extends WP_Widget {

function __construct() {
    parent::__construct(
        ‘my_tabber_widget‘, // Base ID
        ‘My Tabber‘, // Widget name 
        array( ‘description‘ => __( ‘A tabbed widget with custom content‘, ‘text_domain‘ ), ) 
    );
}

// Display the widget in the front-end
public function widget( $args, $instance ) {
    extract( $args );
    $title = apply_filters( ‘widget_title‘, $instance[‘title‘] );

    echo $before_widget; 

    if ( $title ) {
        echo $before_title . $title . $after_title;
    }

    // Enqueue the CSS and JS files
    wp_enqueue_style( ‘tabber-styles‘,  plugin_dir_url( __FILE__ ) . ‘tabber-styles.css‘ );
    wp_enqueue_script( ‘tabber-script‘, plugin_dir_url( __FILE__ ) . ‘tabber-script.js‘, array(‘jquery‘), ‘1.0‘ );

    // Output the tabber HTML
    ?>
    <div class="my-tabber">
        <ul class="tab-nav">
            <li><a href="#tab1">Tab 1</a></li>
            <li><a href="#tab2">Tab 2</a></li>
            <li><a href="#tab3">Tab 3</a></li>
        </ul>

        <div class="tab-content">
            <div id="tab1">
                <h4>Tab 1 content</h4>
                <p>This is the first tab.</p>
            </div>
            <div id="tab2">
                <h4>Tab 2 content</h4>
                <p>This is the second tab.</p> 
            </div>
            <div id="tab3">
                <h4>Tab 3 content</h4>
                <p>This is the third tab.</p>
            </div>
        </div>
    </div>
    <?php 

    echo $after_widget;
}

// Display the widget form in the dashboard
public function form( $instance ) {
    if ( isset( $instance[ ‘title‘ ] ) ) {
        $title = $instance[ ‘title‘ ];
    } else {
        $title = ‘My Tabber‘;
    }
    ?>

    <p>
        <label for="<?php echo $this->get_field_id( ‘title‘ ); ?>"><?php _e( ‘Title:‘ ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id( ‘title‘ ); ?>" name="<?php echo $this->get_field_name( ‘title‘ ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
    </p>

    <?php 
}

// Save the widget options
public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance[‘title‘] = strip_tags( $new_instance[‘title‘] );
    return $instance;
}

}

// Register the widget
function my_tabber_widget_init() {
register_widget( ‘My_Tabber_Widget‘ );
}
add_action( ‘widgets_init‘, ‘my_tabber_widget_init‘ );
?>

Let‘s break down what this code does:

  • The opening block is a standard plugin header defining the plugin‘s name, description, version, etc.
  • The My_Tabber_Widget class extends the built-in WP_Widget class, creating our custom widget.
  • The constructor sets up the widget ID, name, and description.
  • The widget() function outputs the widget‘s HTML, including enqueueing the CSS and JS files. This is what displays in the front-end.
  • The form() function renders the widget options form in the WordPress dashboard.
  • The update() function saves the widget options when the form is submitted.
  • Finally, we register the widget to make it available in the Appearance > Widgets screen.

For this basic example, the actual tab content is hardcoded into the widget HTML. Of course, you can modify this to dynamically generate the content from WordPress posts, custom fields, and so forth. The key pieces are properly enqueueing the scripts/styles and outputting the correct tab and content HTML structure.

Step 3: Code the Widget CSS File

Next, open the tabber-styles.css file and add this code:

.my-tabber {
margin-bottom: 20px;
}

.tab-nav {
list-style: none;
padding: 0;
margin: 0;
overflow: hidden;
background: #f1f1f1;
border-bottom: 1px solid #ccc;
}

.tab-nav li {
float: left;
}

.tab-nav li a {
display: inline-block;
padding: 10px;
color: #000;
text-decoration: none;
}

.tab-nav li a:hover {
background: #ddd;
}

.tab-nav li.active a {
background: #ccc;
}

.tab-content {
padding: 10px;
border: 1px solid #ccc;
border-top: 0;
}

This CSS:

  • Removes default list styling from the tabs navigation
  • Floats the tabs side-by-side
  • Styles the tab links
  • Highlights the active tab
  • Adds spacing and a border around the tab content

Feel free to customize the colors, spacing, borders, and other styles to suit your needs. The main structural styles are floating the tabs and showing/hiding the active tab‘s content.

Step 4: Code the Widget JS File

Finally, open the tabber-script.js file and add the following jQuery code:

jQuery(document).ready(function($){
$(‘.tab-content > div‘).hide();
$(‘.tab-content > div:first-of-type‘).show();
$(‘.tab-nav a‘).click(function(e){
e.preventDefault();
var $this = $(this),
tabgroup = ‘#‘+$this.parents(‘.my-tabber‘).attr(‘id‘),
others = $this.closest(‘li‘).siblings().children(‘a‘),
target = $this.attr(‘href‘);
others.removeClass(‘active‘);
$this.addClass(‘active‘);
$(tabgroup).children(‘.tab-content‘).children(‘div‘).hide();
$(target).show();
});
});

Here‘s what this jQuery code does:

  • Hides all the tab content divs except the first one
  • Listens for clicks on the tab links
  • When a tab is clicked, it:
    • Prevents the default link behavior
    • Removes the "active" class from the other tabs
    • Adds the "active" class to the clicked tab
    • Hides all the tab content divs
    • Shows the div corresponding to the clicked tab

This code assumes a certain HTML structure of the tabs and content divs, which matches the HTML output we created in the widget() function.

With all three plugin files complete, your jQuery tabber widget is ready to use!

Step 5: Install and Activate the Plugin

To make your tabber widget plugin available to your WordPress site:

  1. Upload the entire my-tabber-widget folder to your site‘s wp-content/plugins directory via FTP. Alternatively, you can zip the folder and upload it through the WordPress dashboard via Plugins > Add New > Upload Plugin.

  2. In the Plugins screen of your WP dashboard, find "My Tabber Widget" in the list and click the "Activate" link.

  3. Go to Appearance > Widgets and you should see "My Tabber" in the list of available widgets.

  4. Drag and drop the widget into any sidebar or widget area in your theme.

  5. Add a title if desired and click "Save".

  6. Visit your site‘s front-end and you should see the tabber widget in action!

Customizing the Tabber Widget

With your basic tabber widget up and running, there are many ways you can customize it to better fit your needs:

  1. Widget options: Add more fields to the widget form to let users specify custom tab titles, content, number of tabs, etc. Update the widget() function to use these custom settings.

  2. Dynamic content: Instead of hardcoding the tab content, retrieve it dynamically from WordPress. For instance, create tabs showing your latest posts, most popular products, or custom post types. Use WP_Query or get_posts() to fetch the content within the widget() function.

  3. Styling options: Let users customize the tabber‘s colors, fonts, and spacing to match their theme. Add options to the widget form and adjust the CSS accordingly.

  4. Animation effects: Enhance the tab switching animation with fade or slide effects using jQuery. Modify the tabber-script.js file to apply these additional animations.

Tips and Best Practices for Tabber Widgets

To get the most out of tabber widgets on your WordPress site:

  • Keep tab titles short and descriptive so users can quickly see what content each contains.
  • Limit the number of tabs to around 3-5 to avoid overwhelming visitors with too many options. Use logical categories to group related information under each tab.
  • Ensure the tabber styling matches your overall site design for a cohesive, professional look. Avoid clashing colors or typography.
  • For tabs containing post links, include the post title, excerpt, thumbnail, and/or post meta to give users a helpful preview. Don‘t just dump a ton of bare links.
  • If a tabber has a particularly important tab, consider opening that one by default so its content is immediately visible.
  • Be mindful of the widget‘s height, as it will expand to fit the content of the active tab. Avoid super lengthy tab content that would push the page content down.
  • Test the tabber across various screen sizes and devices to ensure it‘s working responsively. You may need to tweak the CSS to optimize for small mobile displays.

Additional Resources

To dive deeper into jQuery tabbers, custom WordPress widgets, and plugin development, check out the following resources:

Conclusion

Adding an interactive tabbed widget to your WordPress site is an excellent way to organize content, save space, and engage visitors. By creating your own jQuery tabber widget plugin, you have complete control over its appearance and functionality.

We‘ve walked through the basic process of building the plugin, with example PHP, CSS, and jQuery code. You can customize this code to suit your specific needs, or use it as a starting point for more advanced tabber features.

The experience of coding your own widget is also invaluable for leveling up your WordPress development skills. You‘ll gain a deeper understanding of how plugins, widgets, scripts, and styles work together to extend WordPress.

I hope this in-depth tutorial has empowered you to add an awesome jQuery tabber widget to your WordPress site. If you have any questions or suggestions, please leave a comment below. Now go forth and create some tabbable content!

Did you like this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.