Widgets are an integral part of the WordPress ecosystem and play an important role in the structure and layout of a website. By default, WordPress provides a number of widgets such as Text, Image, Recent Posts, Recent Comments among many others. In normal circumstances, these are enough. But in case you want to go to the deep end and provide custom widgets, WordPress accommodates this requirement with the use of its WP_Widget class. By extending this class, we can create and infinite number of custom widgets suiting our needs and requirements. In this article, we are going to take a look at how to create Featured Category Slider Widget in WordPress using Owl Carousel.

Setting up the widget

First of all we need to set up the widget so that it appears in Appearance > Widgets and we are able to set it in one of our sidebars. First of all, in the theme directory, create a new file for including the PHP code for the new widget. You can set it anywhere, but for the sake of this article, i have put it in /inc folder and named it category-slider.php.
Now we need to include it in the theme. Go to functions.php, at the end of the file, add the following code –

This includes the category-slider.php file in the theme. Now, we need to write the PHP code for the widget in the theme. I have already written an article on how to create a custom widget in this article. Do check that out. Also, since I have gone in detail about how to create a widget in this article, I will not be going in detail in this one.

Now, coming to the current article, add the following code to category-slider.php . I’ll explain it in a bit –

In this code, I am registering a widget with the id mytheme_cat_slider by extending the WP_Widget class. The name, id and description of the widget are defined in the constructor of the widget class. The back-end of the Widget ( the fields that appear in Appearance > Widgets ) are defined in the form function while the front-end is present in the widget function.

In the form function, we define the variables that control the title of the widget, the number of posts in the slider and the category to be used for the slider making use of the appropriate checks.

In the update function, we store any change in the variables. Finally, the widget function, we render the output of the widget. I believe the readers of this posts are at least an intermediate level WordPress developers, so I’ll not go in details of the code but I need to point out one thing. In the widget function, we have used the wp_localize_script function. This function is used to transmit PHP variables as an object to a JS file.In our case, we define mytheme-custom-js as our js file. We need JS to render the Owl Carousel later.

Creating the JS file

We need to enqueue the file that we have used in the wp_localize_script function. We create a file named custom.js and place it in js folder in the theme directory. After creating the file, add the following code to functions.php –

In this code, we enqueued the JS file that will incorporate our JS code. note that the first parameter of the wp_localize_script function is same as the ID of the enqueued script. This is how wp_localize_script knows where to transmit the variables object.

Also, we need to enqueue Owl Carousel CSS and JS files. We first need to download them from their official page. We need two files from the downloaded folder. One is the CSS file in dist/assets folder (owl.carousel.min.css), the other in assets folder(owl.carousel.min.js). Place both of these files in the assets/owl folder and add the following code to my_theme_enqueue_scripts() function in functions.php

Understanding what is happening

With all the code here, we need to understand what is happening. In the widget code, the variables get transmitted to custom.js in the form of an object named ‘cat_slider_{id}’ with ID being different for every instance of widget created. This is important as during initialization of slider, as we do not want one slider having multiple controls and vice-versa. In this object, we pass the ID of the widget. Now, the only thing that’s left to do is initializing the slider.

Initialize the Slider

In custom.js, add the following code-

In this code, we have created an array variable named catSliders. Initially this array is empty. On iterating through the window object, the transmitted widget objects are pushed in the catSliders array.

We, then iterate over the catSliders array to initialize the Owl Carousel. In this example, i have kept the settings for the slider constant for all instances. If you want to make them different for every slider instance, they’ll need to be included in the widget and transmitted to custom.js via $exports.

So, that’s about it! Now, when you add the Widget in any sidebar and add number of posts and the category to the widget, the slider will be enabled in the front-end. This is an advanced level article, so if you feel confused at any place, just drop it in the comments. And if you’re a pro, please suggest some improvements to this. There is always room for more efficient code.

Leave a Reply

Your email address will not be published. Required fields are marked *