从POST获取第一个视频(嵌入和视频快捷码)

时间:2015-01-23 作者:Nikita

目标是从帖子中获取第一个视频(嵌入或短代码)。因此,我们需要检查帖子内容是否有嵌入视频(由WP支持)或[视频]短代码。单个帖子中可能存在多个视频和嵌入内容。如果找到了,返回第一个,不取决于要匹配的给定模式的顺序,而是取决于它们插入帖子的顺序。

这是我目前的进展。。。

这个返回第一个[视频]短码。是否有一种方法可以让它不仅查找视频短代码,还可以查找嵌入?

function theme_self_hosted_videos() {
global $post;
$pattern = get_shortcode_regex();
  if ( preg_match_all( \'/\'. $pattern .\'/s\', $post->post_content, $matches ) && array_key_exists( 2, $matches ) && in_array( \'video\', $matches[2] ) ) {
    $videos = $matches[0];
    $i = 0;
    foreach ($videos as $video ) {
        if($i == 0) {
            echo do_shortcode($video);
        }
        $i++;
    }
  }
  return false;
}
add_action( \'wp\', \'theme_self_hosted_videos\' );
这是我目前在返回文章中嵌入的第一个视频函数方面的进展。但并没有按预期工作。显然取决于$pattern\\u数组顺序,也可能取决于模式本身。。。

function theme_oembed_videos() {

global $post;

// Here is a sample array of patterns for supported video embeds from wp-includes/class-wp-embed.php
$pattern_array = array(
    \'#https://youtu\\.be/.*#i\',
    \'#https://(www\\.)?youtube\\.com/playlist.*#i\',
    \'#https://(www\\.)?youtube\\.com/watch.*#i\',
    \'#http://(www\\.)?youtube\\.com/watch.*#i\',
    \'#http://(www\\.)?youtube\\.com/playlist.*#i\',
    \'#http://youtu\\.be/.*#i\',
    \'#https?://wordpress.tv/.*#i\',
    \'#https?://(.+\\.)?vimeo\\.com/.*#i\'
);

foreach ($pattern_array as $pattern) {

    if (preg_match_all($pattern, $post->post_content, $matches)) {
        return wp_oembed_get( $matches[0] );
    }
    return false;

}

}

2 个回复
最合适的回答,由SO网友:bonger 整理而成

您可以尝试将所有模式混合在一个大的regexp或中,然后对内容进行简单的逐行分析:

function theme_oembed_videos() {

    global $post;

    if ( $post && $post->post_content ) {

        global $shortcode_tags;
        // Make a copy of global shortcode tags - we\'ll temporarily overwrite it.
        $theme_shortcode_tags = $shortcode_tags;

        // The shortcodes we\'re interested in.
        $shortcode_tags = array(
            \'video\' => $theme_shortcode_tags[\'video\'],
            \'embed\' => $theme_shortcode_tags[\'embed\']
        );
        // Get the absurd shortcode regexp.
        $video_regex = \'#\' . get_shortcode_regex() . \'#i\';

        // Restore global shortcode tags.
        $shortcode_tags = $theme_shortcode_tags;

        $pattern_array = array( $video_regex );

        // Get the patterns from the embed object.
        if ( ! function_exists( \'_wp_oembed_get_object\' ) ) {
            include ABSPATH . WPINC . \'/class-oembed.php\';
        }
        $oembed = _wp_oembed_get_object();
        $pattern_array = array_merge( $pattern_array, array_keys( $oembed->providers ) );

        // Or all the patterns together.
        $pattern = \'#(\' . array_reduce( $pattern_array, function ( $carry, $item ) {
            if ( strpos( $item, \'#\' ) === 0 ) {
                // Assuming \'#...#i\' regexps.
                $item = substr( $item, 1, -2 );
            } else {
                // Assuming glob patterns.
                $item = str_replace( \'*\', \'(.+)\', $item );
            }
            return $carry ? $carry . \')|(\'  . $item : $item;
        } ) . \')#is\';

        // Simplistic parse of content line by line.
        $lines = explode( "\\n", $post->post_content );
        foreach ( $lines as $line ) {
            $line = trim( $line );
            if ( preg_match( $pattern, $line, $matches ) ) {
                if ( strpos( $matches[0], \'[\' ) === 0 ) {
                    $ret = do_shortcode( $matches[0] );
                } else {
                    $ret = wp_oembed_get( $matches[0] );
                }
                return $ret;
            }
        }
    }
}
此外,您可能希望为嵌入使用瞬态-请参阅this answer 我给了。

SO网友:cybmeta

您可以使用get_media_embedded_in_content() 用于短代码和嵌入媒体的函数(它查找这些HTML标记:音频、视频、对象、嵌入或iframe),只需确保使用应用过滤器和执行短代码的内容即可。

例如:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = apply_filters( \'the_content\', $post->post_content );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];
将上述代码添加到类似于您的函数:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( \'the_content\', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}
要仅限制视频,您可以执行以下操作:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( \'the_content\', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, \'video\' ) || strpos( $embed, \'youtube\' ) || strpos( $embed, \'vimeo\' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}
Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected 波纹管WP 4.2。

结束

相关推荐

Display oEmbed in the_excerpt

我当前正在添加add_filter(\'the_excerpt\', array($wp_embed, \'autoembed\'), 9); 给我的functions.php 如果没有任何其他建议,那就太好了。如果我添加$content-width 至功能。php忽略设置。我有wp-debug 打开,没有错误/警告,也没有记录错误。