注:YouTube和Vimeo的解决方案使用视频提供商提供的特色图像或默认缩略图如果同一页面中存在多个oEmbed,请使用特色图片provoques复制“拇指”在看到更改之前,必须更新帖子要执行的操作:<iframe>
和<img>
尺寸,其他oEmbed灵感的来源
oEmbed, thumbnails and wordpress
用嵌入替换图像
https://stackoverflow.com/q/838878/1287812
插件
<?php
/**
* Plugin Name: oEmbed Replace Iframe with AutoPlay-Image
* Plugin URI: https://wordpress.stackexchange.com/q/73996/12615
* Description: Replaces the iFrame embed with the Featured Image
* and if this not exists replaces with the Video Thumbnail
* Version: 1.0
* Author: brasofilo
* Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo
*/
//avoid direct calls to this file
if (!function_exists (\'add_action\')) {
header(\'Status: 403 Forbidden\');
header(\'HTTP/1.1 403 Forbidden\');
exit();
}
add_filter( \'oembed_dataparse\', \'wpse_73996_oembed_click2play\', 10, 3 );
function wpse_73996_oembed_click2play( $return, $data, $url )
{
// Create Unique ID, case more than one oembed is used in the page
// https://stackoverflow.com/questions/3656713/
$uuid = gettimeofday();
$uuid = mt_rand() . $uuid[\'usec\'];
// Use Featured Image, if exists
// This only works visually if 1 oEmbed per post is used
$post_thumbnail_id = get_post_thumbnail_id( $_REQUEST[\'post\'] );
if( $post_thumbnail_id )
{
$thumb = wp_get_attachment_image_src( $post_thumbnail_id, \'medium\' );
$image = $thumb[0];
}
if( !$image )
$image = $data->thumbnail_url;
// YouTube
if ( $data->provider_name == \'YouTube\' )
{
$autoplay = str_replace(\'feature=oembed\', \'feature=oembed&autoplay=1\', $return );
$return = \'<script type="text/javascript">var embedCode\'
. $uuid . \' = \\\'\'
. $autoplay .\'\\\';</script><div id="videocontainer\'
. $uuid . \'"><img src="\'
. $image
. \'" onclick="document.getElementById(\\\'videocontainer\'
. $uuid . \'\\\').innerHTML = embedCode\'
. $uuid . \';" height="360" width="480" /></div>\';
}
// Vimeo
elseif ( $data->provider_name == \'Vimeo\' )
{
$autoplay = str_replace(\'" width=\', \'?autoplay=1" width=\', $return );
$return = \'<script type="text/javascript">var embedCode\'
. $uuid . \' = \\\'\'
. $autoplay . \'\\\';</script><div id="videocontainer\'
. $uuid . \'"><img src="\'
. $image
.\'" onclick="document.getElementById(\\\'videocontainer\'
. $uuid . \'\\\').innerHTML = embedCode\'
. $uuid . \';" height="360" width="480" /></div>\';
}
return $return;
}
的内容
$data
视频提供商返回
stdClass(
type = \'video\'
version = 1.0
provider_name = \'Vimeo\'
provider_url = \'http://vimeo.com/\'
title = \'Earth\'
author_name = \'Michael König\'
author_url = \'http://vimeo.com/michaelkoenig\'
is_plus = 1
html = \'<iframe src="http://player.vimeo.com/video/32001208" width="540" height="304" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>\'
width = 540
height = 304
duration = 300
description = \'lorem ipsum\'
thumbnail_url = \'http://b.vimeocdn.com/ts/307/031/307031094_295.jpg\'
thumbnail_width = 295
thumbnail_height = 166
video_id = 32001208
)
stdClass(
provider_url = \'http://www.youtube.com/\'
thumbnail_url = \'http://i2.ytimg.com/vi/552yWya5RgY/hqdefault.jpg\'
title = \'Tu cara me suena - Arturo Valls imita a Rihanna\'
html = \'<iframe width="540" height="304" src="http://www.youtube.com/embed/552yWya5RgY?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>\'
author_name = \'antena3\'
height = 304
thumbnail_width = 480
width = 540
version = 1.0
author_url = \'http://www.youtube.com/user/antena3\'
provider_name = \'YouTube\'
type = \'video\'
thumbnail_height = 360
)