Search code examples
phpwordpressshortcode

playing videos in loop/shuffle with php through shortcode


below code play video in page using shortcode : [single_video]

how to make it play shuffle all the videos at that folder in a loop using short code that can pass folder name like : [shuffle_play_gallery folder="naturevideos"]

function display_single_video() {
    $video_url = 'https://website.com/videos/test.mp4';

    // Generate the video element
    $video_element = '<video width="320" height="240" controls>
        <source src="' . $video_url . '" type="video/mp4">
        Your browser does not support the video tag.
        </video>';

    return $video_element;
}

// Register the shortcode to display the single video
add_shortcode('single_video', 'display_single_video');

any help appreciated and

thanks for help


Solution

  • The client (web browser) will need to know a list of available files. You can either:

    1. Embed filenames with the first response
    2. Expose an index somewhere for the client to download

    Afterwards, it's a client-side task. I would do it like this:

    1. Listen for the ended event (documentation)
    2. Swap out the src attribute on your <video> element with the next URL
    3. Call .play() on the element