Easy Video Player Shortcode

Below, you’ll find a detailed guide on how to add the Easy Video Player Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Easy Video Player Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Easy Video Player Plugin and the shortcodes it provides:

Plugin Icon
Easy Video Player

"Easy Video Player is a user-friendly WordPress plugin designed to smoothly integrate videos into your site. It supports various formats, making video embedding a breeze."

★★★★✩ (55) Active Installs: 40000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [evp_embed_video]

Easy Video Player [evp_embed_video] Shortcode

The Easy Video Player shortcode allows users to embed videos with customized settings. It accepts parameters like URL, width, height, autoplay, loop, and more. The shortcode generates a video player with the provided attributes. It also supports user-only video restrictions and schema.org video object markup.

Shortcode: [evp_embed_video]

Parameters

Here is a list of all possible evp_embed_video shortcode parameters and attributes:

  • url – link to the video file to be embedded
  • width – desired width of the video player
  • height – desired height of the video player
  • ratio – aspect ratio of the video player
  • autoplay – auto starts the video once page loads
  • poster – image displayed before video starts
  • loop – makes the video repeat after finishing
  • muted – starts the video without sound
  • controls – shows or hides video controls
  • preload – specifies how the video should be loaded
  • share – enables or disables share button
  • video_id – unique identifier for the video
  • class – allows adding custom classes to the video
  • template – specifies the video player’s template
  • user_only_video – restricts video viewing to logged in users
  • allowed_user_roles – specifies user roles that can view the video
  • schema – adds schema.org structured data to the video
  • name – name of the video for schema
  • description – description of the video for schema
  • duration – duration of the video for schema
  • uploaddate – upload date of the video for schema

Examples and Usage

Basic example – A simple shortcode to embed a video with the default settings.

[evp_embed_video url="http://example.com/video.mp4" /]

Advanced examples

Embed a video with a custom width and height, autoplay enabled, and a custom poster image.

[evp_embed_video url="http://example.com/video.mp4" width="640" height="360" autoplay="true" poster="http://example.com/poster.jpg" /]

Embed a video that is only visible to logged-in users with a certain role, with a custom class for styling, and schema markup for SEO.

[evp_embed_video url="http://example.com/video.mp4" user_only_video="true" allowed_user_roles="subscriber, contributor" class="my-custom-class" schema="true" name="My Video" description="A description of my video." duration="PT1M30S" uploaddate="2021-01-01T00:00:00Z" /]

Embed a video with a custom video ID, loop enabled, muted, no controls, and a 4:3 aspect ratio.

[evp_embed_video url="http://example.com/video.mp4" video_id="my-custom-id" loop="true" muted="true" controls="" ratio="4:3" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [evp_embed_video] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('evp_embed_video', 'evp_embed_video_handler');

Shortcode PHP function:

function evp_embed_video_handler($atts) {
    $atts = shortcode_atts(array(
        'url' => '',
        'width' => '',
        'height' => '',
        'ratio' => '',
        'autoplay' => 'false',
        'poster' => '',
        'loop' => '',
        'muted' => '',
        'controls' => 'controls',
        'preload' => 'metadata',
        'share' => 'true',
        'video_id' => '',
        'class' => '',
        'template' => '',
        'user_only_video' => '',
        'allowed_user_roles' => '',
        'schema' => '',
        'name' => '',
        'description' => '',
        'duration' => '',
        'uploaddate' => '',
    ), $atts);
    $atts = array_map('sanitize_text_field', $atts);
    extract($atts);
    //
    $user_only_video_msg = '';
    $user_only_video_msg = apply_filters('evp_user_only_video', $user_only_video_msg, $atts);
    if(!empty($user_only_video_msg)){
        return $user_only_video_msg;
    }
    //check if mediaelement template is specified
    if($template=='mediaelement'){
        $attr = array();
        $attr['src'] = $url;
        if(is_numeric($width)){
            $attr['width'] = $width;
        }
        if(is_numeric($height)){
            $attr['height'] = $height;
        }
        if ($autoplay == "true"){
            $attr['autoplay'] = 'on';
        }
        if ($loop == "true"){
            $attr['loop'] = 'on';
        }
        if (!empty($poster)){
            $attr['poster'] = $poster;
        }
        if (!empty($preload)){
            $attr['preload'] = $preload;
        }
        $output = wp_video_shortcode($attr);
        $video_schema = '';
        $video_schema = apply_filters('evp_schema', $video_schema, $atts);
        if(!empty($video_schema)){
            $output .= $video_schema;
        }
        return $output;
    }
    //width
    if(!empty($width)){
        $width = ' style="'.esc_attr('max-width:'.$width.'px;').'"';
    }
    else{
        $width = '';
    }
    //custom video id
    if(!empty($video_id)){
        $video_id = ' id="'.esc_attr($video_id).'"';
    }
    //autoplay
    if ($autoplay == "true") {
        $autoplay = " autoplay";
    } else {
        $autoplay = "";
    }
    //loop
    if ($loop == "true") {
        $loop= " loop";
    }
    else{
        $loop= "";
    }
    //muted
    if($muted == "true"){
        $muted = " muted";
    }
    else{
        $muted = "";
    }
    //poster
    if(!empty($poster)){
        $poster = ' data-poster="'.esc_url($poster).'"';
    }
    else{
        $poster = '';
    }
    //controls
    if(isset($controls) && empty($controls)){
        $controls = "";
    }
    else{
        $controls = " controls";
    }
    //ratio only allows 16:9/4:3
    /*
    if($ratio == "4:3"){
        $ratio = "4:3";
    }
    else{
        $ratio = "16:9";
    }*/
    //class
    if(!empty($class)){
        $class = ' class="easy-video-player '.esc_attr($class).'"';
    }
    else{
        $class = ' class="easy-video-player"';
    }
    $icon_url = EASY_VIDEO_PLAYER_URL.'/lib/plyr.svg';
    $blank_video = EASY_VIDEO_PLAYER_URL.'/lib/blank.mp4';
    $video_id = "plyr" . uniqid(); 
    $ratio_code = '16:9';
    if(isset($ratio) && !empty($ratio)){
        $ratio_code = $ratio;
    }
    $video_output = '
    <div'.$width.'>        
    <video id="'.$video_id.'"'.$autoplay.$loop.$muted.$poster.$controls.$class.'>
       <source src="'.esc_url($url).'" type="video/mp4" />
    </video>
    </div>';
    $script_output = <<<EOT
    <script>
        const evplayer{$video_id} = new Plyr(document.getElementById('$video_id'));
        evplayer{$video_id}.ratio = '{$ratio_code}';
        evplayer{$video_id}.iconUrl = '{$icon_url}';
        evplayer{$video_id}.blankVideo = '{$blank_video}';  
    </script>
EOT;
    $output = $video_output.$script_output;
    $video_schema = '';
    $video_schema = apply_filters('evp_schema', $video_schema, $atts);
    if(!empty($video_schema)){
        $output .= $video_schema;
    }
    return $output;
}

Code file location:

easy-video-player/easy-video-player/easy-video-player.php

Conclusion

Now that you’ve learned how to embed the Easy Video Player Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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