Dynamically add Items to Video Slider Module

Written by Dan Mossop

Divi comes with a video slider module that lets uses select from a collection of videos. Normally, you have to add these videos within the video slider module settings. If you instead want to dynamically populate the video slider from a list of video URLs, here's how to do it.

Step 1: Add PHP Code

First, we'll add some PHP code that will do the heavy lifting for us. You only need to add it once, and shouldn't need to modify it. It's main function is to scan the content of the page for Divi's video slider shortcode and add a video slide item for each of an array of videos.

class DBCDynamicVideoSlider {

    public function __construct($id, array $videos) {
        $this->id = $id;
        $this->videos = $videos;
    }

    public function init() {
        add_filter('the_content', array($this, 'modify_slider'));
    }

    function modify_slider($content) {
        return preg_replace_callback(
            '/(\[et_pb_video_slider [^]]*module_id="'.preg_quote($this->id, '/').'"[^]]*\])(.*?)(\[\/et_pb_video_slider\])/is', 
            array($this, 'add_slides'), 
            $content
        );
    }

    function add_slides($match) {
        $items = array();
        foreach($this->videos as $url) {
            $items[] = str_replace(
                ' src="https://www.youtube.com/watch?v=FkQuawiGWUw"', 
                ' src="'.esc_attr($url).'"',
                $match[2]
            );
        }
        return $match[1].implode('', $items).$match[3];
    }
}

Step 2: Set up the Video Slider Module

First, add a video slider module to your page. It should by default have a single video, called "New Item" configured with the URL for a video about Divi, i.e.:

Leave that video configured as is. You can style it if you like, but leave the Divi video in place. Our code below will use the item as a template. Don't delete the "New Item" video, or add any other items.

Now give the Video Slider module a CSS ID of (for example) "dynamic-slider" at:

Video Slider Settings > Advanced > CSS ID & Classes > CSS ID

Like so:

Step 3: Load the Videos via PHP

Now we need to call our earlier PHP code with the CSS ID of our slider and a list of videos to use. Here I'm just specifying the videos in a PHP array directly, but in a more realistic scenario these might be pulled in from a custom field, or retrieved from an external API.

$videos = array(
    'https://www.youtube.com/watch?v=cbP2N1BQdYc', // cat video #1
    'https://www.youtube.com/watch?v=lZqqMtW87hU', // cat video #2
    'https://www.youtube.com/watch?v=jk0is7dZrK0', // cat video #3
    'https://www.youtube.com/watch?v=uHKfrz65KSU', // cat video #4
);

(new DBCDynamicVideoSlider('dynamic-slider', $videos))->init();

Step 4: View the Result

If all is well, we should now see our videos displayed in the video slider on the front-end, e.g.:

We may earn a commission when you visit links on our website.

Run PHP Code Directly in Your Divi Layouts

Unlock endless customization and dynamic functionality by seamlessly adding PHP code to your Divi pages. Style, preview, and debug your PHP creations directly in the visual builder with robust error handling and enhanced security. Perfect for implementing solutions like dynamically adding videos to your sliders.

Latest Posts

Add Text Before the Number in the Divi Circle Counter

Adding a custom number prefix to your Circle Counter lets you clarify what the value represents—such as currency, units, or labels—without relying on the default percentage format. This helps your counters better match real-world metrics and keeps the design...

Add a Second Line of Text in a Divi Button Module

Adding a second line of text beneath a button’s main label lets you provide extra context, a brief benefit statement, or a supporting call-to-action without cluttering your design. This can improve clarity and increase clicks by making the button’s purpose more...

Top (or Bottom) Align Divi Icon List Module Icons

When Icon List items wrap onto multiple lines, the default icon alignment can make the icon appear vertically centered next to the text, which may look unbalanced or harder to scan. Setting the icons to align at the top creates a cleaner, more consistent...

Make Divi Button Modules Full Width with Centered Content

This configuration makes a Divi button stretch to the full width of its container while keeping its label neatly centered, creating a bold, edge-to-edge call-to-action. You might want this for hero sections, pricing tables, or any layout where a standard-sized button...

Fix the Flickr Typo in the Divi Social Media Follow Module

Sometimes it's the small things that make the difference... there is a minor typo in the Divi Social Media Follow module that was present in Divi 4, and (currently) remains in Divi 5. Specifically, the tooltip for Flickr is misspelled as "Flikr". Here's how to fix...

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

2 Comments

  1. Hi Dan, Thanks for this! I actually need just one video to be shown. But this code probably won't work on the Video module. If I only add 1 video, will it just show 1 without 3 empty fields below?
    Thanks, Elisabeth

    Reply
    • Hey Elisabeth, you're right, it won't work on the Video module without modification. But in the Video slider if you just change the last block of code to just specify one video like so:

      $videos = array(
          'https://www.youtube.com/watch?v=cbP2N1BQdYc', // cat video #1
      );
      (new DBCDynamicVideoSlider('dynamic-slider', $videos))->init();
      

      Then it should just add a single slide to the Video slider (without adding any empty fields). I haven't tested directly with this code yet, but normally when you add a single slide to the Video Slider module it just displays the same way as the Video module does – i.e. the video with no navigation arrows or thumbnails.

      So I think that should do what you want. If it doesn't work the way you expect, though, give me a shout and I'll take a look for you. Thanks!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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