1

我将如何过滤 WordPress oEmbed 提供商列表?我的目标是只允许 Twitter 和 Youtube。

编辑:我可以做这样的事情吗?

function filter_oembed_provider_list( $array ) {
    $array = array( 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed',                     false ) );
    return $array;
}
add_filter( 'oembed_providers', 'filter_oembed_provider_list' );

但这似乎不起作用。

请看class-oembed.php中的相关代码:

apply_filters( 'oembed_providers', array(
        '#https?://(www\.)?youtube\.com/watch.*#i'           => array( 'http://www.youtube.com/oembed',                     true  ),
        'http://youtu.be/*'                                  => array( 'http://www.youtube.com/oembed',                     false ),
        'http://blip.tv/*'                                   => array( 'http://blip.tv/oembed/',                            false ),
        '#https?://(www\.)?vimeo\.com/.*#i'                  => array( 'http://vimeo.com/api/oembed.{format}',              true  ),
        '#https?://(www\.)?dailymotion\.com/.*#i'            => array( 'http://www.dailymotion.com/services/oembed',        true  ),
        'http://dai.ly/*'                                    => array( 'http://www.dailymotion.com/services/oembed',        false ),
        '#https?://(www\.)?flickr\.com/.*#i'                 => array( 'http://www.flickr.com/services/oembed/',            true  ),
        'http://flic.kr/*'                                   => array( 'http://www.flickr.com/services/oembed/',            false ),
        '#https?://(.+\.)?smugmug\.com/.*#i'                 => array( 'http://api.smugmug.com/services/oembed/',           true  ),
        '#https?://(www\.)?hulu\.com/watch/.*#i'             => array( 'http://www.hulu.com/api/oembed.{format}',           true  ),
        '#https?://(www\.)?viddler\.com/.*#i'                => array( 'http://lab.viddler.com/services/oembed/',           true  ),
        'http://qik.com/*'                                   => array( 'http://qik.com/api/oembed.{format}',                false ),
        'http://revision3.com/*'                             => array( 'http://revision3.com/api/oembed/',                  false ),
        'http://i*.photobucket.com/albums/*'                 => array( 'http://photobucket.com/oembed',                     false ),
        'http://gi*.photobucket.com/groups/*'                => array( 'http://photobucket.com/oembed',                     false ),
        '#https?://(www\.)?scribd\.com/.*#i'                 => array( 'http://www.scribd.com/services/oembed',             true  ),
        'http://wordpress.tv/*'                              => array( 'http://wordpress.tv/oembed/',                       false ),
        '#https?://(.+\.)?polldaddy\.com/.*#i'               => array( 'http://polldaddy.com/oembed/',                      true  ),
        '#https?://(www\.)?funnyordie\.com/videos/.*#i'      => array( 'http://www.funnyordie.com/oembed',                  true  ),
        '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i'=> array( 'http://api.twitter.com/1/statuses/oembed.{format}', true  ),
        '#https?://(www\.)?soundcloud\.com/.*#i'             => array( 'http://soundcloud.com/oembed',                      true  ),
        '#https?://(www\.)?slideshare\.net/*#'               => array( 'http://www.slideshare.net/api/oembed/2',            true  ),
        '#http://instagr(\.am|am\.com)/p/.*#i'               => array( 'http://api.instagram.com/oembed',                   true  ),
        '#https?://(www\.)?rdio\.com/.*#i'                   => array( 'http://www.rdio.com/api/oembed/',                   true  ),
        '#https?://rd\.io/x/.*#i'                            => array( 'http://www.rdio.com/api/oembed/',                   true  ),
        '#https?://(open|play)\.spotify\.com/.*#i'           => array( 'https://embed.spotify.com/oembed/',                 true  ),
    ) );
4

3 回答 3

4
于 2013-10-19T13:47:01.320 回答
1

如果您提供的过滤器 ( oembed_providers) 对您有用,那么您可以尝试以下操作:

/**
 * Filter the oembed providers through a whitelist
 *
 * @param array $providers
 * @return array $providers
 */
function filter_oembed_provider_list( $providers )
{
    // edit the whitelist to your needs
    $whitelist = array( 'youtu', 'twitter' );

    $output = array();

    foreach( $providers as $key => $provider )
    {
        foreach( $whitelist as $allowed )
        {
            if( stristr( $key, $allowed ) )
                $output[$key] = $provider;

        }
    }
    return $output; 
}

add_filter( 'oembed_providers' , 'filter_oembed_provider_list', 99 );

您必须在其中$whitelist根据需要进行编辑。

更新:

感谢@Ivan Hanák,建议保存帖子部分;-)

在您更新 oEmbed 缓存后,通过保存帖子,此代码段现在应该可以工作了;-)

于 2013-10-19T15:26:49.853 回答
0

我也有同样的需求,因为 instagram Oembeds 在 4.4 之前的 Wordpress 中被破坏了。我意识到提供者的过滤器在你的主题初始化之前运行,所以如果你想添加这个过滤器,你必须使用一个插件。这就是我用的

<?php
/**
 * Plugin Name: Fix Instagram oEmbed
 * Plugin URI: https://10up.com
 * Description: Fix Instagram oEmbed.
 * Author: 10up
 * Version: 1.0.0
 * Author URI: https://10up.com
 * License: GPL2
 */

namespace TenUp\Plugin\InstagramFix;
add_filter( 'oembed_providers', __NAMESPACE__ . '\\oembed_providers' );


function oembed_providers( $providers ) {
    if ( ! isset( $providers['#https?://(www\.)?instagr(\.am|am\.com)/p/.*#i'] ) ) {
        $providers['#https?://(www\.)?instagr(\.am|am\.com)/p/.*#i'] = array(
            'https://api.instagram.com/oembed',
            true
        );
    }
    return $providers;
}

所以你必须

1)在插件文件夹中创建一个类似 instagram-oembed-fix 的文件夹
2)创建一个名为 instagram-oembed-fix.php 的文件
3)复制上面的 php 代码
4)激活您的插件(网络中的网络激活)
5)通过转到任何帖子并按“保存”来重新生成 oEmbed 缓存

于 2015-11-28T14:44:03.987 回答