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

WordPress后台编辑器上传视频的时候已通过视频短代码返回到内容里的,这样子有些小程序没法识别,所以需要把短代码改成video标签。将以下代码加入到主题的functions.php里:

remove_shortcode  'video'  'wp_video_shortcode'  ); 
add_shortcode  'video'  'MBT_video_shortcode'  ); 
函数MBT_video_shortcode $ attr $ content =  ''   { 
全局$ content_width ; 
$ post_id = get_post () 吗?get_the_ID ()  0 ;

静态$ instance =  0 ; 
$ instance ++; 
$ override = apply_filters  'wp_video_shortcode_override'  '' $ attr $ content $ instance ); 
如果  ''  !== $ override  { 
返回$ override ; 
}

$ video =  null ;

$ default_types = wp_get_video_extensions (); 
$ defaults_atts = array 
'src'  =>  '' 
'poster'  =>  `` 
'loop'  =>  '' 
'autoplay'  =>  `` 
'preload'  =>  '元数据' 
'宽度'  = >  640 
'height'  =>  360 
//'class'=>'wp-video-shortcode',
);

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

$的ATT = shortcode_atts $ defaults_atts $ ATTR  '视频'  );

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' ]   ); 
如果  $ is_youtube &&  $ is_vimeo  {
$ type = wp_check_filetype $ atts [ 'src' ],wp_get_mime_types () ); 
if   in_array strtolower $ type [ 'ext' ]  ),$ default_types   { 
返回sprintf  '<a class="wp-embedded-video" href="%s">%s </a>' esc_url $ atts [ 'src' ]  ),esc_html $ atts [ 'src' ]   );
} 
}

如果 $ 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 () ); 
如果 strtolower $ type ['ext' ]  )) === $ ext  { 
$ primary =  true ; 
} 
} 
} 
}

如果  $ primary  { 
$ videos = get_attached_media  'video' $ post_id ); 
if  $ videos   { 
返回; 
}

$ video = 重置$ videos ); 
$ atts [ 'src' ]  = wp_get_attachment_url $ video- > ID ); 
if  $ atts [ 'src' ]    { 
返回; 
}

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),
//'宽度'=> absint($ atts ['width']),
//'height'=> absint($ atts ['height']),
'poster'  => esc_url $ atts [ 'poster' ]  ),
'循环'  => 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 ]    {未
设置$ 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  { 
如果  $ atts [ $ fallback ]    { 
if  $ fileurl   { 
$ fileurl = $ atts [ $ fallback ]; 
} 
如果  '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  '_' $ 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 ;

返回apply_filters  'MBT_video_shortcode' $ output $ atts $ video $ post_id $ library ); 
}

评论0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
没有账号? 忘记密码?