Create a custom module to compare images with a slider
In this tutorial, you'll create a module plugin for Page Builder that lets you add a third-party jQuery plugin called TwentyTwenty to your layouts.
The TwentyTwenty jQuery plugin by ZURB compares two images with a sliding bar, as shown in the following animation. This is useful for comparing two images with the same dimensions.
You can see a working example and all the code contained in this tutorial at https://github.com/Pross/custom-module-example-twentytwenty.
Create the plugin
Create the folders, files, and code for WordPress to recognize the plugin correctly, display the information on the WordPress plugins admin screen, and load the module correctly.
- Navigate to your WordPress plugins folder, typically wp-content/plugins/, and create a new subfolder named custom-module-example-twentytwenty.
- Create a file in that subfolder called fl-module-twentytwenty.php.
- Add the following code to create the name, description, and version information that WordPress uses on the plugins admin screen.
- Add the code for the main plugin class, which will load the module file:
Create the module folders and files
- In the custom-module-example-twentytwenty folder, create a subfolder called modules.
- Inside that folder, create another folder called twentytwenty.
This will be the folder for the Page Builder module's code and assets.
- Now create a new file in your
twentytwenty module folder called twentytwenty.php.
This will be the module file.
Register the module
Add the following code to twentytwenty.php.
This code extends the
FLBuilderModuleclass to register the module as a BeaverBuilder module.
Add module assets
Put three jQuery plugin files into the right folders and add code for the module assets.
- Download the TwentyTwenty zip file from https://zurb.com/playground/twentytwenty.
- Unzip the file.
- In the modules/twentytwenty folder create two new folders named css and js.
- Upload twentytwenty.css into the css folder.
- Upload jquery.twentytwenty.js and jquery.event.move.js into the js folder.
- Open the module file again and add the following code into the
This code enqueues the assets properly when the module is on the page.
Add module options
The module will have two options, one for each photo. The following code uses the
FLBuilder::register_modulemethod to register the two options.
Add the following code to the bottom of twentytwenty.php.
Create frontend.php to render the module
For any Page Builder module to render, it needs a frontend.php file.
- Create a new folder called includes inside the modules/twentwenty folder.
- Create a new file frontend.php in the includes folder.
- Add the following code.
This will be the HTML output by the module when it is inserted into a layout.
As you can see this code simply creates a
<div>with the two classes
twentytwenty-containerand includes the two images using the settings we previously registered.
The TwentyTwenty jQuery plugin will do the rest.
Create frontend.js.php to tie it together
Now add the jQuery snippet needed to make all this work.
- Create a file named frontend.js.php in the includes folder.
- Add the following code.
Test your new module
- In the WordPress admin panel, click Plugins and find your new plugin listed. As determined by the code you added in Task 1, the plugin name is Beaver Builder TwentyTwenty Module.
- Activate the plugin.
- Open a page for editing in Page Builder and click the Plus button to open the Content panel.
- Search for TwentyTwenty or scroll to the last section labeled Example modules and try using your new module in a layout
If you can't see the images, clear your caches.