我正在尝试为我的amp页面格式化wordpress博客的视频。我在content editor中使用的代码是:
[video width="768" height="576" mp4="https://mywebsite.com/wp-content/uploads/20**/0*/video-name.mp4"][/video]
我想以以下格式在AMP页面中显示此视频:
<amp-video controls
width="640"
height="360"
layout="responsive">
<source src="https://mywebsite.com/wp-content/uploads/20**/0*/video-name.mp4" />
</amp-video>
我尝试了以下功能,但它们对我不起作用。
Try 1:
function am_video_amp_format($content){
global $post;
$pattern ="~<video(.*?)></video>~i";
$replacement = \'<amp-video controls
width="640"
height="360"
layout="responsive">
<source src="$2" />
</amp-video>\';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter(\'the_content\', \'am_video_amp_format\');
Try 2:
function am_video_amp_format($content){
global $post;
$pattern ="~<iframe(.*?)>(.*?)<video(.*?)src=\\"(.*?)\\">(.*?)</video>(.*?)</iframe>~i";
$replacement = \'<amp-video controls
width="640"
height="360"
layout="responsive">
<source src="$4" />
</amp-video>\';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter(\'the_content\', \'am_video_amp_format\');
Try 3:
function am_video_amp_format($content){
global $post;
$pattern ="~<video(.*?)><source(.*?)src=\\"(.*?)\\"(.*?)></video>~i";
$replacement = \'<amp-video controls
width="640"
height="360"
layout="responsive">
<source src="$2" />
</amp-video>\';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter(\'the_content\', \'am_video_amp_format\');
我还能得到高度和宽度吗?
非常感谢。
最合适的回答,由SO网友:Levi Dulstein 整理而成
所以你在编辑器中使用默认的WordPress视频短代码,对吗?如果是这样的话,就不需要过滤整个内容,尤其是视频的标记已经呈现出来,而且相当复杂:
<div style="width: 960px;" class="wp-video"><!--[if lt IE 9]>
<script>document.createElement(\'video\');</script><![endif]-->
<video class="wp-video-shortcode" id="video-160186-1" width="960" height="600" preload="metadata" controls="controls">
<source type="video/mp4" src="YOUR_VIDEO_URL" />
<a href="YOUR_VIDEO_URL">YOUR_VIDEO_URL</a>
</video>
</div>
因此,您可以使用
wp_video_shortcode
滤器然后就很容易了:
function change_video_markup_to_amp( $output, $atts, $video, $post_id, $library ) {
/*
change output only on \'post\' post type, you might wanna get
rid of this if you want to change video markup everywhere
on your site
*/
if ( ! \'post\' === get_post_type( $post_id ) ) {
return $output;
}
/*
get video data, you can also check other $atts array
keys for different video formats, by default you\'ll find:
\'mp4\', \'m4v\', \'webm\', \'ogv\', \'flv\'.
*/
$video_url = ! empty( $atts[\'mp4\'] ) ? $atts[\'mp4\'] : \'\';
$height = ! empty( $atts[\'height\'] ) ? $atts[\'height\'] : \'\';
$width = ! empty( $atts[\'width\'] ) ? $atts[\'width\'] : \'\';
// return default shortcode output if no video url is found
if ( empty( $video_url ) ) {
return $output;
}
// now put the amp markup together
$amp_output = sprintf( \'<amp-video controls width="%1$d" height="%2$d" layout="responsive"><source src="%3$s" /></amp-video>\', absint( $width ), absint( $height ), esc_url( $video_url ) );
return $amp_output;
}
add_filter( \'wp_video_shortcode\', \'change_video_markup_to_amp\', 10, 5 );