How to add Swiper Slider in WordPress Themes without plugins

Sliders are an important element of modern websites and play an important part in making the website appear more interactive and engaging. When it comes to sliders, there are many options available based on your requirement and expertise on working with third-party code and libraries. One such popular slider library is the SwiperJS Slider. It is one of the most popular slider libraries around and is used by some of the world’s most popular brands on their websites and millions of users worldwide. In this article, we’re going to take a look at how to integrate Swiper slider in WordPress.

Why use Swiper Slider in WordPress?

Swiper slider is a powerful slider library used to add custom sliders in websites and mobile apps. It is quite popular with developers and for good reasons. Some of the amazing features offered bt SwiperJS include –

Native JavaScript

One of the prie reasons for swiper slider being the first choice of web developers is the fact that it’s built completely using native JavaScript without any libraries such as jQuery making it smaller in size and very efficient.

Highly Customizable

The slider is highly customizable with large array of properties and methods which we can use to customize the functioning of the slider as per our requirements. You can check out their official demo page to check what’s possible using SwiperJS.

Fully Responsive

The slider is fully responsive and adapts seamlessly to smaller screens. This makes swiper a great choice for use in mobile apps and websites.

Dynamic and Virtual Slides

Another feature of SwiperJS making it favorite of large companies is its use of virtual and dynamic slides. This keeps DOM size for the page low. Because of this, it is perfect for applications with large amount of content.

Apart from the above mentioned features, SwiperJS offers a lot more in terms of functionality and accessibility. Image Lazy Loading, parallax transitions, thumb control are just some of the amazing features offered by swiper slider.

How to Add Swiper Slider in WordPress?

Pre-requisites

Before we start with integrating Swiper slider in WordPress, I’m assuming you have a working knowledge of HTML, CSS and JS.

HTML: Providing the structure to slider and its content.

CSS: Styling the layout and structure of the slider to integrate it coherently with the project.

JS: Initializing and customizing the slider to integrate with the project.

Here’s a pen of what we will be creating in this article –

Adding the Slider Files

Firstly, we need to add the slider files in our project. Primarily, there are 3 ways we can add Swiper slider in our WordPress website –

using npm

We can use this method if our project uses a module system or project bundler such as WebPack. We just add Swiper directly in our JS file like so –

This method is not natively supported in a conventional WordPress installation. In order to use this method, we need to install and set up Webpack in our theme directory. This method is a bit contrived and not relevant for this article.

using CDN

This is perhaps the easiest method to add swiper slider in wordPress. We can add the required swiper slider files by calling it from a CDN. So, swiper slider files are hosted on cdnjs.com. While there are a bunch of files, we just need two of them – swiper-bundle.min.js and swiper-bundle.min.css. Add the following code to the functions.php file in your theme –

After saving, go to the browser. You’ll notice swiper-bundle.min.js and swiper-undle.min.css have been added to the <head>  tag. It should look something like this –

hosting slider locally

This is my go-to method whenever I have to integrate WordPress in a WordPress project. Hosting files locally prevents that precious HTTP Request to a third-party server to fetch slider resources. In order to integrate swiper slider using this method, go ahead and download the above mentioned swiper-bundle.min.js and swiper-bundle.min.css from its CDN library. After downloading, save it in your theme directory somewhere it’s easily accessible. For the sake of this article, we save it in assets/swiper directory.

After saving the files, add the following code to functions.php  file in your theme directory

Save the file and refresh the browser. You’ll see the respective files added in <head>  like so –

Adding HTML

Since we have added the slider now, it’s time to add the HTML for the slider.For this article’s sake, I’m adding it in index.php but you add it wherever you plan on adding your slider. Add the following code to index.php

In the above code, we add 5 images wrapped in <figure> tag. These images are then wrapped in a div with .swiper-wrapper class which is further wrapped by .slider-wrapper div. Note that we have added .swiper class to the .slider-wrapper div. This is required for the slider to work properly.

Including CSS for the Slider

Now, we need to add some CSS for the slider so that it looks a little more structured. Add the following code to theme’s style.css file –

Initializing Slider with JS

Now that we have added the HTML and some CSS to make it a litte more structured, let’s go ahead and initialize the slider using JavaScript. For that, we need to create a file in which we put the slider’s code. Create a new file in assets folder titled custom.js. We need to enqueue this file. For that we need to modify the functions.php file where we added the slider.

Go to functions.php and modify the my_theme_enqueue_scripts() function like so –

Note that in the code above, we have added the highlighted code to enqueue custom.js. Note the third argument in wp_enqueue_script is set to ['swiper-js'] . It is essential to make sure the swiper-bundle.min.js file is a dependency of custom.js file and loads before custom.js.

We are going to add our code initialize slider in this file. Add the following code to custom.js file –


If you have been following the tutorial correctly up to now, you should see the slider in action in the front-end. But it will be a single image per slide. That is not very elegant. We need to customize the slider.

Customizing the Slider

To add customizations, we need to add a second options argument to our code above. Modify it like so –

In the code above, we added a second options  argument adding custom options to modify the slider as per our requirements. Note that these are pretty basic customizations and you can customize it further as per your requirements and preferences. For complete list of options, please refer to swiper slider’s API reference.

Bonus

By now, we have a working slider. We can add navigation in the form of “Previous” and “Next” buttons. Also, we can add pagination buttons in the bottom. It is quite easy. We just need to modify the slider’s HTML. Go ahead and modify the slider’s HTML like so –

Note that we added div with .swiper-pagination class after the .swiper-wrapper slider element to add pagination. Also, to add navigation, we added divs with .swiper-button-prev  and .swiper-button-next classes to act as arrows.

Finally, we modify our slider initialization in custom.js to include support for pagination and navigation like so –

Here, we added navigation and pagination properties to our options  argument. Note that this is the default documentation and it could customized easilyusing the Slider API.

Conclusion

So, that was it! In this article, we saw how to integrate Swiper Slider in WordPress. From adding the files to initializing the slider, we went through the whole process. In the end, we saw how we can add pagination as well as navigation to the slider.

SwiperJS is a very powerful library with plenty of features and customizations making it a favourite of developers and businesses.

Hope you were able to learn something new in this article. If you liked it, do check out our blog where we regularly post informative articles about JavaScript and WordPress gelping our readers advance their web development skills. See you in next article!

Meet the Author

Divjot Singh

Divjot is an experienced WordPress developer and technical writer with more than a decade of experience in WordPress Development. Whenever he's not creating amazing WordPress experiences, he can be found on the road with his bike.

Leave a Reply

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