Add Lightbox Script to Your WordPress Blog

There are bunch of WordPress plugins to handle Lightbox / popup / modal into your blog easily. But doing it manually is always a challenge. I love working with Magnific Popup recently, not just its fast and lightweight script, also it free to use. I also use this script in one of my plugin.

Get The Script and Upload

Now go get the script, visit the author’s page. You can also use the Build Tool to get the script in the minified and optimized version. Copy the code to a new file and save it as jquery.magnific-popup.min.js

buildtool

 

Next, we should get the CSS file, you can get it here, and save it as magnific-popup.css.  The last one is to create custom call script, create new file and name it magnific-init.js. I added this code in it.

Above script says that if class name “magn-popup” found will run magnific popup script. As an example:

Next thing is to upload it to your active theme. I put them all in a folder named “magnific”.

Integration to WordPress

After you have both files uploaded. Next thing is to add the script into theme and call it by modify the functions.php file. Create a function, add this script to the end of functions.php:

The JS scripts above will be loaded in footer, but the css will be loaded in header.

Next is to set the call, there are methods to set the call by adding class to the tab manually,  set a filter to the_content, or even to add the hook to media library, but I will give you the simple and safest solution is by using jQuery.

The theory is to add the class name “magn-popup” to every image link. So every time some one click on it, it will trigger the magnific popup to run. Add the following function to  your functions.php file:

The above function will add class name “magn-popup” to all of your image links with png. gif, and jpg  file formats. The script must be called before jquery.magnific-popup.min.js, so I added it to the header.

The above method will also work on another lightbox scripts available, either Fancybox, FrescoJS, Pretty Photo, you name it.

Cheers.

How To, jQuery, UI / UX Design, Wordpress

Any idea? Fill the comment below

This site uses Akismet to reduce spam. Learn how your comment data is processed.