WP Oemed未通过“Autoplay=1”变量传递

时间:2010-12-14 作者:criticerz

我遇到了一个问题:

我正在通过自定义字段传递此信息:http://www.youtube.com/watch?v=E6P1Q-UycHA&autoplay=1

(注意autoplay=1)

但是当我用wp\\u oembed\\u get加载我的主题视频时。。。它可以很好地显示视频,但不收听autoplay=1 我正在通过的变量。

我需要在加载页面时播放视频。

有什么想法吗?

谢谢Alain Fontaine

7 个回复
SO网友:Rarst

这些并不是像YouTube那样真正的论据,更多的是WordPress本身的论据。

处理它的一种方法是稍后在过滤器中访问您的参数并修改HTML输出。

传入参数数组:

wp_oembed_get( \'http://www.youtube.com/watch?v=\', array( \'autoplay\' => 1 ) );
和筛选器:

add_filter(\'oembed_result\',\'oembed_result\', 10, 3);

function oembed_result($html, $url, $args) {

    // $args includes custom argument
    // modify $html as you need

    return $html;
}

SO网友:jlengstorf

我知道这个问题已经很老了,但我有一个可行的解决方案,我想它可能会对其他试图在谷歌搜索中找到这些信息并发现这些信息是空的人有所帮助。

此解决方案已在WordPress 3.7.1+:

/**
 * Adds the `autoplay` query string argument to embedded YouTube videos
 */
function wpse5362_autoplay_youtube_oembed( $provider, $url, $args ) {
    if (strpos($provider, \'youtube\')!==FALSE) {
        $provider = add_query_arg(\'autoplay\', 1, $provider);
    }

    return $provider;
}
add_filter(\'oembed_fetch_url\', \'wpse5362_autoplay_youtube_oembed\', 10, 3);
在主题中添加以上内容functions.php 使所有通过oEmbed添加的YouTube视频自动播放。

这是如何工作的class-oembed.php 在第212行fetch() 方法应用筛选器oembed_fetch_url 允许修改URL。

为了避免向其他oEmbed提供者添加无用的参数,我们在提供者URL中检查“youtube”——我们还可以检查“vimeo”和/或其他视频服务——如果存在该字符串,则添加autoplay 查询字符串的参数。

SO网友:Federico

这是我在函数方面的解决方案。php

function embed_responsive_autoplay($code){
    if(strpos($code, \'youtu.be\') !== false || strpos($code, \'youtube.com\') !== false){
        $return = preg_replace(\'@embed/([^"&]*)@\', \'embed/$1&showinfo=0&autoplay=1\', $code);
        return \'<div class="embed-container">\' . $return . \'</div>\';
    }
    return \'<div class="embed-container">\' . $code . \'</div>\';
}

add_filter( \'embed_oembed_html\', \'embed_responsive_autoplay\');
add_filter( \'video_embed_html\', \'embed_responsive_autoplay\' ); // Jetpack
享受

SO网友:Laust Deleuran

Cross-posting my answer from https://stackoverflow.com/a/55053642/799327, hope that\'s okay since it\'s from the same OP:


So, after some research on this, the best way to do this is to leverage the oembed_fetch_url filter hook to add extra arguments to the oEmbed request URL. My specific goal was to allow an autoplay paramater, but this method is built to be easy to tailor to any oEmbed argument you need.

First, add this to your functions.php:

<?php
/**
 * Add parameters to embed
 * @src https://foxland.fi/adding-parameters-to-wordpress-oembed/
 * @src https://github.com/WordPress/WordPress/blob/ec417a34a7ce0d10a998b7eb6d50d7ba6291d94d/wp-includes/class-oembed.php#L553
 */
$allowed_args = [\'autoplay\'];

function koa_oembed_args($provider, $url, $args) {
    global $allowed_args;

    $filtered_args = array_filter(
        $args,
        function ($key) use ($allowed_args) {
            return in_array($key, $allowed_args);
        },
        ARRAY_FILTER_USE_KEY
    );

    foreach ($filtered_args as $key => $value) {
        $provider = add_query_arg($key, $value, $provider);
    }

    return $provider;
}

add_filter(\'oembed_fetch_url\', \'koa_oembed_args\', 10, 3);

This function takes the generated oEmbed URL and its corresponding arguments and checks it agains a hard-coded list of whitelisted arguments, in this case [\'autoplay\']. If it sees any of these whitelisted keywords in the arguments passed to the oEmbed filter, it adds them with their given value to the oEmbed URL.

Then, all you need to do is add the oEmbed parameter to your shortcode in the Wordpress editor, like this:

[embed autoplay="true"]https://vimeo.com/1234567890/1234567890[/embed]

Be aware that the oEmbed class in WP uses the postmeta as a cache for these requests, so if you\'ve embedded the target URL before, you might have to clear your postmeta cache in some way or add a cache buster of some kind to the target URL. If the link is in the cache, the filter hook will never get to run.

I hope this makes sense, as I feel like it\'s a pretty useful feature that\'s surprisingly hard to figure out how to achieve.

SO网友:t31os

任何东西after the first parameter 必须作为参数数组发送。

参见codex页面上给出的示例
http://codex.wordpress.org/Function_Reference/wp_oembed_get

如果您试图获取自定义字段值,并让嵌入代码将其转换为视频(即,如果将其放入内容中会发生什么),那么您可以执行以下操作。。。

/*
   N - replace with a valid post ID
   key - replace with a valid custom field key(name)
*/
$data = get_post_meta( N, \'key\', true );
if( $data )
   echo apply_filters( \'the_content\', $data );
当然,这会对您不需要的数据运行多个过滤器,但它的作用是一样的。。

SO网友:achchu93

经过研究,我发现了这一点。代码如下。

function iweb_modest_youtube_player( $html, $url, $args ) {
    return str_replace( \'?feature=oembed\', \'? 
    feature=oembed&modestbranding=1&showinfo=0&rel=0\', $html );
}
add_filter( \'oembed_result\', \'iweb_modest_youtube_player\', 10, 3 );
贷项至https://gist.github.com/mustardBees/7704269#file-functions-php.

谢谢

SO网友:interimpulso

通过修改wp includes/media中的wp\\u oembed\\u get()函数,可以轻松解决此问题。php对此:

function wp_oembed_get( $url, $args = \'\' ) {
    // Manually build the IFRAME embed with the related videos option disabled and autoplay turned on
    if(preg_match("/youtube.com\\/watch\\?v=([^&]+)/i", $url, $aMatch)){
        return \'<iframe width="560" height="315" src="http://www.youtube.com/embed/\' . $aMatch[1] . \'?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>\';
    }

    require_once( ABSPATH . WPINC . \'/class-oembed.php\' );
    $oembed = _wp_oembed_get_object();
    return $oembed->get_html( $url, $args );
}

结束

相关推荐

Displaying oEmbed errors?

有时,通过oEmbed嵌入项目是不可能的,例如,当YouTube视频已禁用嵌入时。The oEmbed service will return a 401 Unauthorized, 并且不会转换代码。有没有办法通知用户这一点?当前的工作流是非直观的(至少对我来说),我更喜欢在WordPress页面上,或者更好的是,在编辑器中显示一条消息,说明对象无法嵌入。