• g******* 加入了本站
  • d****g 签到打卡,获得1积分奖励
  • d****g 加入了本站
  • u******* 签到打卡,获得1积分奖励
  • a***n 登录了本站
  • z*****e 登录了本站
  • z*****e 登录了本站
  • z*****e 购买了资源 测试音乐付费听
  • z*****e 签到打卡,获得1积分奖励
  • z*****e 加入了本站
所有分类
  • 所有分类
  • 未分类
  • 初创
  • 应用

WordPress 修改后台编辑器上传视频的[video]短代码为video标签

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( '<a class="wp-embedded-video" href="%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" class="wp-video">%s</div>', $width_rule, $html );
$output = $html;

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

原文链接:http://demo.mobantu.com/modown/blog/758,转载请注明出处。

0
分享海报

评论1

请先
  1. :evil:
    卡巴 2019-09-05 0
演示收费观看视频,支持视频列表
演示收费观看视频,支持视频列表
3分钟前 有人购买 去瞅瞅看

站点公告

2021.06.09起演示站暂时不再提供在线支付测试,仅演示功能,如测试付费下载可联系模板兔。了解此主题
显示验证码
没有账号?注册  忘记密码?

社交账号快速登录

微信扫一扫关注
关注后请回复“登录”二字获取验证码