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

    1. <?php
    2. remove_shortcode('video', 'wp_video_shortcode');
    3. add_shortcode('video', 'MBT_video_shortcode');
    4. function MBT_video_shortcode($attr, $content = '') {
    5. global $content_width;
    6. $post_id = get_post() ? get_the_ID() : 0;
    7. static $instance = 0;
    8. $instance++;
    9. $override = apply_filters('wp_video_shortcode_override', '', $attr, $content, $instance);
    10. if ('' !== $override) {
    11. return $override;
    12. }
    13. $video = null;
    14. $default_types = wp_get_video_extensions();
    15. $defaults_atts = array('src' => '', 'poster' => '', 'loop' => '', 'autoplay' => '', 'preload' => 'metadata', 'width' => 640, 'height' => 360,
    16. //'class' => 'wp-video-shortcode',
    17. );
    18. foreach ($default_types as $type) {
    19. $defaults_atts[$type] = '';
    20. }
    21. $atts = shortcode_atts($defaults_atts, $attr, 'video');
    22. if (is_admin()) {
    23. if ($atts['width'] > $defaults_atts['width']) {
    24. $atts['height'] = round(($atts['height'] * $defaults_atts['width']) / $atts['width']);
    25. $atts['width'] = $defaults_atts['width'];
    26. }
    27. } else {
    28. if (!empty($content_width) && $atts['width'] > $content_width) {
    29. $atts['height'] = round(($atts['height'] * $content_width) / $atts['width']);
    30. $atts['width'] = $content_width;
    31. }
    32. }
    33. $is_vimeo = $is_youtube = false;
    34. $yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
    35. $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';
    36. $primary = false;
    37. if (!empty($atts['src'])) {
    38. $is_vimeo = (preg_match($vimeo_pattern, $atts['src']));
    39. $is_youtube = (preg_match($yt_pattern, $atts['src']));
    40. if (!$is_youtube && !$is_vimeo) {
    41. $type = wp_check_filetype($atts['src'], wp_get_mime_types());
    42. if (!in_array(strtolower($type['ext']), $default_types)) {
    43. return sprintf('<a class="wp-embedded-video" href="%s">%s</a>', esc_url($atts['src']), esc_html($atts['src']));
    44. }
    45. }
    46. if ($is_vimeo) {
    47. wp_enqueue_script('mediaelement-vimeo');
    48. }
    49. $primary = true;
    50. array_unshift($default_types, 'src');
    51. } else {
    52. foreach ($default_types as $ext) {
    53. if (!empty($atts[$ext])) {
    54. $type = wp_check_filetype($atts[$ext], wp_get_mime_types());
    55. if (strtolower($type['ext']) === $ext) {
    56. $primary = true;
    57. }
    58. }
    59. }
    60. }
    61. if (!$primary) {
    62. $videos = get_attached_media('video', $post_id);
    63. if (empty($videos)) {
    64. return;
    65. }
    66. $video = reset($videos);
    67. $atts['src'] = wp_get_attachment_url($video->ID);
    68. if (empty($atts['src'])) {
    69. return;
    70. }
    71. array_unshift($default_types, 'src');
    72. }
    73. $library = apply_filters('wp_video_shortcode_library', 'mediaelement');
    74. if ('mediaelement' === $library && did_action('init')) {
    75. wp_enqueue_style('wp-mediaelement');
    76. wp_enqueue_script('wp-mediaelement');
    77. wp_enqueue_script('mediaelement-vimeo');
    78. }
    79. if ('mediaelement' === $library) {
    80. if ($is_youtube) {
    81. $atts['src'] = remove_query_arg('feature', $atts['src']);
    82. $atts['src'] = set_url_scheme($atts['src'], 'https');
    83. } elseif ($is_vimeo) {
    84. $parsed_vimeo_url = wp_parse_url($atts['src']);
    85. $vimeo_src = 'https://' . $parsed_vimeo_url['host'] . $parsed_vimeo_url['path'];
    86. $loop = $atts['loop'] ? '1' : '0';
    87. $atts['src'] = add_query_arg('loop', $loop, $vimeo_src);
    88. }
    89. }
    90. $atts['class'] = apply_filters('wp_video_shortcode_class', $atts['class'], $atts);
    91. $html_atts = array(
    92. //'class' => $atts['class'],
    93. //'id' => sprintf( 'video-%d-%d', $post_id, $instance ),
    94. //'width' => absint( $atts['width'] ),
    95. //'height' => absint( $atts['height'] ),
    96. 'poster' => esc_url($atts['poster']), 'loop' => wp_validate_boolean($atts['loop']), 'autoplay' => wp_validate_boolean($atts['autoplay']),
    97. //'preload' => $atts['preload'],
    98. );
    99. foreach (array('poster', 'loop', 'autoplay', 'preload') as $a) {
    100. if (empty($html_atts[$a])) {
    101. unset($html_atts[$a]);
    102. }
    103. }
    104. $attr_strings = array();
    105. foreach ($html_atts as $k => $v) {
    106. $attr_strings[] = $k . '="' . esc_attr($v) . '"';
    107. }
    108. $html = '';
    109. $fileurl = '';
    110. foreach ($default_types as $fallback) {
    111. if (!empty($atts[$fallback])) {
    112. if (empty($fileurl)) {
    113. $fileurl = $atts[$fallback];
    114. }
    115. if ('src' === $fallback && $is_youtube) {
    116. $type = array('type' => 'video/youtube');
    117. } elseif ('src' === $fallback && $is_vimeo) {
    118. $type = array('type' => 'video/vimeo');
    119. } else {
    120. $type = wp_check_filetype($atts[$fallback], wp_get_mime_types());
    121. }
    122. $url = add_query_arg('_', $instance, $atts[$fallback]);
    123. }
    124. }
    125. $html.= sprintf('<video %s src="' . esc_url($url) . '" controls="controls">', join(' ', $attr_strings));
    126. $html.= '</video>';
    127. $width_rule = '';
    128. if (!empty($atts['width'])) {
    129. $width_rule = sprintf('width: %dpx;', $atts['width']);
    130. }
    131. //$output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html );
    132. $output = $html;
    133. return apply_filters('MBT_video_shortcode', $output, $atts, $video, $post_id, $library);
    134. }