【问题标题】:YouTube link to Iframe?YouTube 链接到 iframe?
【发布时间】:2017-04-23 09:04:22
【问题描述】:

我想要一个用于 WordPress 的函数,该函数将从链接中获取 YouTube id,并自动将其转换为 iframe 嵌入代码到自定义字段 video_url 中。

video_url = https://www.youtube.com/watch?v=UqV4uGLQ2L0

有时 OEmbed 无法在我的 WordPress 网站上运行,因此我正在寻找此解决方案。

这个:

https://www.youtube.com/watch?v=UqV4uGLQ2L0

应该自动以这种方式包装:

<iframe width="560" height="315" src="https://www.youtube.com/embed/UqV4uGLQ2L0" frameborder="0" allowfullscreen></iframe>

【问题讨论】:

  • ,你想在同一个窗口打开视频吗?
  • 不要在任何地方打开,我只需要将自定义字段值 video_url 链接到 single.php 内的完整 iframe 的 php 代码,因此我实际上并没有更改 single.php 值。
  • 你能给我举个例子吗?
  • 我使用自定义字段值 video_url = youtube.com/watch?v=UqV4uGLQ2L0 创建帖子,现在在帖子的前端,我调用自定义字段,我希望它在 iframe 中通过functions.php。跨度>
  • 你想创建一个链接或者存储链接

标签: php wordpress youtube


【解决方案1】:

第 1 步: 检索名为 video url 的 custom_field 的值:

$unescpaed_url = get_post_meta( $post->ID, 'video_url', true );

第二步:检索后转义url值:

if ( $unescpaed_url) {
    // Returns an empty string for invalid URLs
    $url = esc_url( 'http://' . $unescpaed_url );

    if ( '' !== $url ) {
        $display = esc_html( $unescpaed_url );

        print "<a href='$url' target='_blank'>$display</a>";
    }
}

【讨论】:

    【解决方案2】:

    使用下面的函数,它将返回 youtube 嵌入链接。

    将您的 youtube url 传递给函数,然后它将返回嵌入代码链接。

    在functions.php中

    function parseVideos($videoString = null)
    {   
        if (strpos($videoString, 'youtube.com/embed') !== FALSE) 
        {
            return $videoString;
        }
        if (strpos($videoString, 'iframe') !== FALSE) 
        {
            // retrieve the video url
            $anchorRegex = '/src="(.*)?"/isU';
            $results = array();
            if (preg_match($anchorRegex, $video, $results)) 
            {
                $link = trim($results[1]);
            }
        } 
        else 
        {
            // we already have a url
            $link = $videoString;
        }    
    
        if (strpos($link, 'youtube.com') !== FALSE) { 
    
            preg_match(
            '/[\\?\\&]v=([^\\?\\&]+)/',
            $link,
            $matches
            );  
            //the ID of the YouTube URL: x6qe_kVaBpg
            $id = $matches[1];
            return '//www.youtube.com/embed/'.$id;
        }
        else if (strpos($link, 'youtu.be') !== FALSE) { 
            preg_match(
            '/youtu.be\/([a-zA-Z0-9_]+)\??/i',
            $link,
            $matches
            );  
            $id = $matches[1];
            return '//www.youtube.com/embed/'.$id;
        }
        else if (strpos($link, 'player.vimeo.com') !== FALSE) {
            // works on:
            // http://player.vimeo.com/video/37985580?title=0&amp;byline=0&amp;portrait=0
            $videoIdRegex = '/player.vimeo.com\/video\/([0-9]+)\??/i';
            preg_match($videoIdRegex, $link, $matches);
            $id = $matches[1];  
            return '//player.vimeo.com/video/'.$id;
        }
        else if (strpos($link, 'vimeo.com') !== FALSE) { 
            //extract the ID
            preg_match(
                    '/\/\/(www\.)?vimeo.com\/(\d+)($|\/)/',
                    $link,
                    $matches
                );
    
            //the ID of the Vimeo URL: 71673549 
            $id = $matches[2];  
            return '//player.vimeo.com/video/'.$id;
        }
        return $videoString;
        // return data
    
    }
    

    【讨论】:

    • 它不起作用。是否可能是因为我的视频链接在自定义字段 video_url 内?
    猜你喜欢
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多