2

I'm having this problem.

I am passing this through a custom field here

(notice the "autoplay=1")

But when I load the video on my theme using wp_oembed_get... it displays the video fine, but it does not listen to the autoplay=1 variable I am passing through.

I need the videos to play on the load of the page.

4

5 回答 5

6

我认为这样做的方法是使用 wordpress 过滤器:

function modify_youtube_embed_url($html) {
    return str_replace("?feature=oembed", "?feature=oembed&autoplay=1", $html);
}
add_filter('oembed_result', 'modify_youtube_embed_url');
于 2013-05-28T20:54:43.967 回答
1

这是我在functions.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

享受!

于 2014-12-12T16:33:54.733 回答
0

查找函数 wp_oembed_get 并使用 args 传递自动播放...应该可以正常工作。只需粘贴视频的 url 而不是 &autoplay... 你将把它编码到函数的 args 部分。

于 2011-06-01T04:36:34.553 回答
0

因此,在对此进行一些研究之后,最好的方法是利用oembed_fetch_url过滤器挂钩向 oEmbed 请求 URL 添加额外的参数。我的具体目标是允许自动播放参数,但此方法易于根据您需要的任何 oEmbed 参数进行定制。

首先,将此添加到您的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);

此函数获取生成的 oEmbed URL 及其相应的参数,并再次检查白名单参数的硬编码列表,在本例中为['autoplay']. 如果它在传递给 oEmbed 过滤器的参数中看到任何这些列入白名单的关键字,它会将它们及其给定值添加到 oEmbed URL。

然后,您需要做的就是在 Wordpress 编辑器中将 oEmbed 参数添加到您的简码中,如下所示:

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

请注意,WP 中的 oEmbed 类使用 postmeta 作为这些请求的缓存,因此如果您之前嵌入了目标 URL,您可能需要以某种方式清除您的 postmeta 缓存或添加某种缓存破坏器到目标网址。如果链接在缓存中,过滤器挂钩将永远无法运行。

我希望这是有道理的,因为我觉得这是一个非常有用的功能,但很难弄清楚如何实现。

于 2019-03-07T22:13:09.317 回答
-3

这可以通过修改 wp-includes/media.php 中的 wp_oembed_get() 函数来轻松解决:

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 );
}
于 2012-04-12T07:31:55.893 回答