Last updated on October 18, 2023

Divi video background

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Add video backgrounds to Divi sections.

The Divi theme is a popular WordPress theme that allows you to create stunning websites with ease. One of its standout features is the ability to add video backgrounds to sections or modules. This can be a great way to make your website more engaging and visually appealing.

To add a video background in Divi, you can use the built-in Video Background module. Here’s an example of how you can use this module to add a video background to a section:

<?php
function wpsnippets_add_divi_video_background() {
    if ( function_exists( 'et_pb_register_module' ) ) {
        et_pb_register_module(
            'VideoBackground',
            array(
                'title'       => __( 'Video Background', 'et_builder' ),
                'categories'  => array( 'media' ),
                'slug'        => 'et_pb_video_background',
                'render_callback' => 'wpsnippets_render_divi_video_background',
            )
        );
    }
}
add_action( 'et_builder_ready', 'wpsnippets_add_divi_video_background' );

function wpsnippets_render_divi_video_background( $attrs, $content = null, $render_slug ) {
    ob_start();
    ?>
    <div class="et_pb_video_background">
        <!-- Your video code goes here -->
    </div>
    <?php
    return ob_get_clean();
}

In this code snippet, we first register a custom module called “VideoBackground” using the et_pb_register_module() function. We specify the module’s title, categories, slug, and a render callback function.

The render callback function wpsnippets_render_divi_video_background() is responsible for generating the HTML output for the module. In this example, we simply wrap the video code within a <div> element with the class “etpbvideo_background”.

To use this module, you can add a new section in the Divi Builder, then add a new module and select the “Video Background” module. You can then customize the module settings and add your video code within the <div> element.

Note: The video code itself is not included in this example, as it depends on the specific video source and format you want to use. You can add your own video code, such as an HTML5 video tag or an embedded YouTube video, within the <div> element.

This code snippet can be useful when you want to add a video background to a section or module in your Divi-powered website. It allows you to extend the functionality of Divi by creating a custom module specifically for video backgrounds.

Examples

Example 1: Adding a Video Background to a Divi Section

This use case demonstrates how to add a video background to a Divi section using the Divi Builder’s built-in options.

function wpsnippets_add_divi_video_background() {
    echo '<div class="video-background">';
    echo '<video autoplay loop muted>';
    echo '<source src="path/to/video.mp4" type="video/mp4">';
    echo '</video>';
    echo '</div>';
}
add_action( 'et_before_main_content', 'wpsnippets_add_divi_video_background' );

In this example, we create a custom function wpsnippets_add_divi_video_background() that echoes the HTML markup for a video background. We then hook this function to the et_before_main_content action, which ensures that the video background is added before the main content of the Divi theme. You can replace "path/to/video.mp4" with the actual path to your video file.

Example 2: Adding a YouTube Video Background to a Divi Section

This use case demonstrates how to add a YouTube video background to a Divi section using a custom PHP function.

function wpsnippets_add_youtube_video_background() {
    echo '<div class="video-background">';
    echo '<iframe src="https://www.youtube.com/embed/VIDEO_ID?autoplay=1&loop=1&mute=1&controls=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
    echo '</div>';
}
add_action( 'et_before_main_content', 'wpsnippets_add_youtube_video_background' );

In this example, we create a custom function wpsnippets_add_youtube_video_background() that echoes the HTML markup for a YouTube video background. We then hook this function to the et_before_main_content action, which ensures that the video background is added before the main content of the Divi theme. Replace "VIDEO_ID" in the src attribute with the actual YouTube video ID.

Example 3: Adding a Vimeo Video Background to a Divi Section

This use case demonstrates how to add a Vimeo video background to a Divi section using a custom PHP function.

function wpsnippets_add_vimeo_video_background() {
    echo '<div class="video-background">';
    echo '<iframe src="https://player.vimeo.com/video/VIDEO_ID?autoplay=1&loop=1&title=0&byline=0&portrait=0" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe>';
    echo '</div>';
}
add_action( 'et_before_main_content', 'wpsnippets_add_vimeo_video_background' );

In this example, we create a custom function wpsnippets_add_vimeo_video_background() that echoes the HTML markup for a Vimeo video background. We then hook this function to the et_before_main_content action, which ensures that the video background is added before the main content of the Divi theme. Replace "VIDEO_ID" in the src attribute with the actual Vimeo video ID.

Last updated on October 18, 2023. Originally posted on December 16, 2023.

Leave a Reply

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