最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Video shortcode - autoplay all videos

programmeradmin2浏览0评论

How can I autoplay all videos added by video shortcode?
I can make only one of them autoplay if only one has autoplay attribute set to 1, and others to 0, otherwise all are stopped.

How can I autoplay all videos added by video shortcode?
I can make only one of them autoplay if only one has autoplay attribute set to 1, and others to 0, otherwise all are stopped.

Share Improve this question asked Feb 22, 2014 at 14:34 DomagojDomagoj 1551 gold badge2 silver badges8 bronze badges 3
  • Why would you play multiple video at the same time? – s_ha_dum Commented Feb 22, 2014 at 14:43
  • i'm using them like backgrounds on two sections.. – Domagoj Commented Feb 22, 2014 at 14:53
  • There is pauseOtherPlayers: false option in mediaelement, but how to pass that from WP? – Domagoj Commented Feb 22, 2014 at 14:54
Add a comment  | 

4 Answers 4

Reset to default 4

I ran into this problem while trying to make HTML videos behave like GIFs. WordPress's built-in video player uses HTML video elements but does not allow videos to play simultaneously.

Instead of using the default WordPress video player (which is best used for more standard video content), I chose to manually use the <video> element via a custom shortcode. For the best compatibility (especially on mobiles) we should also make sure the videos are also muted.

After adding the following code, simply use:

[videogif mp4="http://path_to_file.mp4"]

And you'll have a nice video working. To control styling and add (basic) HTML video controls, use something like:

[videogif mp4="http://path_to_file.mp4" style='width: 80%' controls='1']

Code

Add to your functions.php:

// Video gif shortcode
function videogif($atts = [])
{
    // normalize attribute keys, lowercase
    $atts = array_change_key_case((array)$atts, CASE_LOWER);

    // override default attributes with user attributes
    $wporg_atts = shortcode_atts([
            'mp4' => $atts['mp4'],
            'style' => null,
            'controls' => False
        ], $atts);

    // build output
    $o = '';
    $o .= '<video autoplay loop muted playsinline ';
    if ($wporg_atts['controls']) $o .= 'controls ';
    $o .= 'class="videogif"';
    if (!is_null($wporg_atts['style'])) $o .= 'style="' . $wporg_atts['style'] . '" ';
    $o .= '><source src="' . $wporg_atts['mp4'] . '" type="video/mp4" />';
    $o .= '<p>Your browser does not support the video element.</p></video>';

    // return output
    return $o;
}
add_shortcode( 'videogif', 'videogif' );

I also use the following CSS to resize and center the videos by default:

/* Center videogif by default */
.videogif {
    width: 100%;
    display:block;
    margin: 0 auto;
}

You could try to use the shortcode_atts_video filter:

add_filter( 'shortcode_atts_video', 'overwrite_video_atts_wpse', 10,3 );

function overwrite_video_atts_wpse( $out, $pairs, $atts )
{
    // force the autoplay video shortcode attribute to ON:
    $out['autoplay'] = 1; 

    // force the autoplay video shortcode attribute to OFF:
    //$out['autoplay'] = 0; 

    return $out;
}

to overwrite the [video] shortcode attributes.

I have done this with my own video shortcode, in which i changed classes, so I can initiate player with $('.my-video-shortcode').mediaelementplayer( {pauseOtherPlayers: false} );
It does what I needed :)

I realise this post is old however birgire response no longer works

Instead of

$out['autoplay'] = '1';

which makes sense, it should actually be

$out['autoplay'] = 'on';

add_filter( 'shortcode_atts_video', 'overwrite_video_atts_wpse', 10,3 );

function overwrite_video_atts_wpse( $out, $pairs, $atts )
{
    // force the autoplay video shortcode attribute to ON:
    $out['autoplay'] = 'on'; 

    // force the autoplay video shortcode attribute to OFF:
    //$out['autoplay'] = 0; 

    return $out;
}
发布评论

评论列表(0)

  1. 暂无评论