WP Oembed 未通过“autoplay=1”;多变的

发布于 2024-10-08 06:06:17 字数 281 浏览 0 评论 0原文

我有这个问题。

我通过自定义字段此处

(注意“autoplay=1”)

但是当我使用wp_oembed_get在我的主题上加载视频时......它显示视频正常,但它不听自动播放=1 我正在传递的变量。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只怪假的太真实 2024-10-15 06:06:17

我认为做到这一点的方法是使用 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');

I think the way to do it is using wordpress filters:

function modify_youtube_embed_url($html) {
    return str_replace("?feature=oembed", "?feature=oembed&autoplay=1", $html);
}
add_filter('oembed_result', 'modify_youtube_embed_url');
没有伤那来痛 2024-10-15 06:06:17

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

享受吧!

This is my solution in 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

Enjoy!

孤蝉 2024-10-15 06:06:17

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

look up function wp_oembed_get and use the args to pass the autoplay... should work fine. Just paste in the url of the video not the &autoplay... you'll code that into the args part of the function.

め七分饶幸 2024-10-15 06:06:17

因此,经过对此进行一些研究后,最好的方法是利用 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 中。

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

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

请注意,WP 中的 oEmbed 类使用 postmeta 作为这些请求的缓存,因此如果您已嵌入目标 URL在此之前,您可能必须以某种方式清除 postmeta 缓存,或者向目标 URL 添加某种缓存破坏器。如果链接在缓存中,则过滤器挂钩将永远不会运行。

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

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.

靖瑶 2024-10-15 06:06:17

通过将 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 );
}

This can be easily fixed by modifying the wp_oembed_get() function in wp-includes/media.php to this:

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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文