WordPress 后台编辑器上传视频的时候是以video短代码返回到内容里的,这样子有些小程序没法识别,所以需要把短代码改成video标签。

将以下代码加入到主题的functions.php里:

remove_shortcode( \'video\', \'wp_video_shortcode\' );
add_shortcode( \'video\', \'MBT_video_shortcode\' );
function MBT_video_shortcode( $attr, $content = \'\' ) {
global $content_width;
$post_id = get_post() ? get_the_ID() : 0;

static $instance = 0;
$instance++;
$override = apply_filters( \'wp_video_shortcode_override\', \'\', $attr, $content, $instance );
if ( \'\' !== $override ) {
return $override;
}

$video = null;

$default_types = wp_get_video_extensions();
$defaults_atts = array(
\'src\' => \'\',
\'poster\' => \'\',
\'loop\' => \'\',
\'autoplay\' => \'\',
\'preload\' => \'metadata\',
\'width\' => 640,
\'height\' => 360,
//\'class\' => \'wp-video-shortcode\',
);

foreach ( $default_types as $type ) {
$defaults_atts[$type] = \'\';
}

$atts = shortcode_atts( $defaults_atts, $attr, \'video\' );

if ( is_admin() ) {
if ( $atts[\'width\'] > $defaults_atts[\'width\'] ) {
$atts[\'height\'] = round( ( $atts[\'height\'] * $defaults_atts[\'width\'] ) / $atts[\'width\'] );
$atts[\'width\'] = $defaults_atts[\'width\'];
}
} else {
if ( ! empty( $content_width ) && $atts[\'width\'] > $content_width ) {
$atts[\'height\'] = round( ( $atts[\'height\'] * $content_width ) / $atts[\'width\'] );
$atts[\'width\'] = $content_width;
}
}

$is_vimeo = $is_youtube = false;
$yt_pattern = \'#^https?://(?:www\\.)?(?:youtube\\.com/watch|youtu\\.be/)#\';
$vimeo_pattern = \'#^https?://(.+\\.)?vimeo\\.com/.*#\';

$primary = false;
if ( ! empty( $atts[\'src\'] ) ) {
$is_vimeo = ( preg_match( $vimeo_pattern, $atts[\'src\'] ) );
$is_youtube = ( preg_match( $yt_pattern, $atts[\'src\'] ) );
if ( ! $is_youtube && ! $is_vimeo ) {
$type = wp_check_filetype( $atts[\'src\'], wp_get_mime_types() );
if ( ! in_array( strtolower( $type[\'ext\'] ), $default_types ) ) {
return sprintf( \'<ahref=\"%s\">%s</a>\', esc_url( $atts[\'src\'] ), esc_html( $atts[\'src\'] ) );
}
}

if ( $is_vimeo ) {
wp_enqueue_script( \'mediaelement-vimeo\' );
}

$primary = true;
array_unshift( $default_types, \'src\' );
} else {
foreach ( $default_types as $ext ) {
if ( ! empty( $atts[ $ext ] ) ) {
$type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() );
if ( strtolower( $type[\'ext\'] ) === $ext ) {
$primary = true;
}
}
}
}

if ( ! $primary ) {
$videos = get_attached_media( \'video\', $post_id );
if ( empty( $videos ) ) {
return;
}

$video = reset( $videos );
$atts[\'src\'] = wp_get_attachment_url( $video->ID );
if ( empty( $atts[\'src\'] ) ) {
return;
}

array_unshift( $default_types, \'src\' );
}

$library = apply_filters( \'wp_video_shortcode_library\', \'mediaelement\' );
if ( \'mediaelement\' === $library && did_action( \'init\' ) ) {
wp_enqueue_style( \'wp-mediaelement\' );
wp_enqueue_script( \'wp-mediaelement\' );
wp_enqueue_script( \'mediaelement-vimeo\' );
}

if ( \'mediaelement\' === $library ) {
if ( $is_youtube ) {
$atts[\'src\'] = remove_query_arg( \'feature\', $atts[\'src\'] );
$atts[\'src\'] = set_url_scheme( $atts[\'src\'], \'https\' );
} elseif ( $is_vimeo ) {
$parsed_vimeo_url = wp_parse_url( $atts[\'src\'] );
$vimeo_src = \'https://\' . $parsed_vimeo_url[\'host\'] . $parsed_vimeo_url[\'path\'];

$loop = $atts[\'loop\'] ? \'1\' : \'0\';
$atts[\'src\'] = add_query_arg( \'loop\', $loop, $vimeo_src );
}
}

$atts[\'class\'] = apply_filters( \'wp_video_shortcode_class\', $atts[\'class\'], $atts );

$html_atts = array(
//\'class\' => $atts[\'class\'],
//\'id\' => sprintf( \'video-%d-%d\', $post_id, $instance ),
//\'width\' => absint( $atts[\'width\'] ),
//\'height\' => absint( $atts[\'height\'] ),
\'poster\' => esc_url( $atts[\'poster\'] ),
\'loop\' => wp_validate_boolean( $atts[\'loop\'] ),
\'autoplay\' => wp_validate_boolean( $atts[\'autoplay\'] ),
//\'preload\' => $atts[\'preload\'],
);

foreach ( array( \'poster\', \'loop\', \'autoplay\', \'preload\' ) as $a ) {
if ( empty( $html_atts[$a] ) ) {
unset( $html_atts[$a] );
}
}

$attr_strings = array();
foreach ( $html_atts as $k => $v ) {
$attr_strings[] = $k . \'=\"\' . esc_attr( $v ) . \'\"\';
}

$html = \'\';
$fileurl = \'\';
foreach ( $default_types as $fallback ) {
if ( ! empty( $atts[ $fallback ] ) ) {
if ( empty( $fileurl ) ) {
$fileurl = $atts[ $fallback ];
}
if ( \'src\' === $fallback && $is_youtube ) {
$type = array( \'type\' => \'video/youtube\' );
} elseif ( \'src\' === $fallback && $is_vimeo ) {
$type = array( \'type\' => \'video/vimeo\' );
} else {
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
}
$url = add_query_arg( \'_\', $instance, $atts[ $fallback ] );
}
}

$html .= sprintf( \'<video %s src=\"\'.esc_url( $url ).\'\" controls=\"controls\">\', join( \' \', $attr_strings ) );

$html .= \'</video>\';

$width_rule = \'\';
if ( ! empty( $atts[\'width\'] ) ) {
$width_rule = sprintf( \'width: %dpx;\', $atts[\'width\'] );
}
//$output = sprintf( \'<div style=\"%s\" >%s</div>\', $width_rule, $html );
$output = $html;

return apply_filters( \'MBT_video_shortcode\', $output, $atts, $video, $post_id, $library );
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注