首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤WordPress oEmbed提供程序列表

过滤WordPress oEmbed提供程序列表
EN

Stack Overflow用户
提问于 2013-10-19 13:10:34
回答 3查看 1.9K关注 0票数 1

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

编辑:,我可以这样做吗?

代码语言:javascript
复制
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' );

但这似乎行不通。

请参阅类-oEmbed.php中的相关代码:

代码语言:javascript
复制
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  ),
    ) );
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-19 13:47:01

有一个内置函数wp_oembed_remove_provider,分别是wp_oembed_add_provider.

编辑

添加到functions.php文件

代码语言:javascript
复制
function customize_oembed() {

    //load oembed class
    require_once ABSPATH . DIRECTORY_SEPARATOR . 'wp-includes' . DIRECTORY_SEPARATOR . 'class-oembed.php';

    //get a singleton object
    $oembed = _wp_oembed_get_object();

    /**
     * Forget about those lines
     *
       //empty the providers list
       $oembed->providers = array();

       //add what you want
       wp_oembed_add_provider( 'http://site.com/watchvideo/*', 'http://site.com/oembedprovider' );  
    */

    //use applying a filter
    $providers = array(
        'youtube' => array( 'http://www.youtube.com/oembed', false), 
        'twitter'=> array( 'http://api.twitter.com/1/statuses/oembed.{format}', true  )
    );
    $oembed->providers = apply_filters('oembed_providers', $providers);
}
add_action('init', 'customize_oembed');

编辑2

我查看了它,并发现,您可以使用过滤器oembed_providers的方式完全相同,就像它在类的构造函数中使用一样。因此,如果您可以在一个函数中使用额外的wordpress函数,这将再次尝试要求类文件并实例化单个对象,这是无用的。

(尽管它能工作,但我仍然不能使用这个过滤器:-D)

编辑3

终于,我让它起作用了。

它与您的代码非常相似,尽管函数中的参数基本上是无用的。它只为您提供要重写的原始提供程序,因此您没有使用它,您可以直接返回数组。

也许重要的是,在编辑列表之后,您需要更新您的帖子,因为wordpress正在将一些数据保存到*_postmeta下的_oembed_…键下。

代码语言:javascript
复制
function filter_oembed_provider_list( ) {
    return array( 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ) );
}
add_filter( 'oembed_providers', 'filter_oembed_provider_list' );
票数 4
EN

Stack Overflow用户

发布于 2013-10-19 15:26:49

如果您提供的筛选器(oembed_providers)正在为您工作,那么您可以尝试如下所示:

代码语言:javascript
复制
/**
 * 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缓存之后,通过保存post,这个片段应该可以工作;)

票数 1
EN

Stack Overflow用户

发布于 2015-11-28 14:44:03

我也有同样的需求,因为在4.4之前,instagram Oembed在Wordpress中就被打破了。我意识到,提供程序的过滤器在主题初始化之前运行,所以如果要添加此筛选器,就必须使用plugin.This。

代码语言:javascript
复制
<?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-oEmbedd-Fix.php的文件。

3)复制上面的php代码

4)激活你的插件(网络激活)

5)重新生成oEmbed缓存,方法是访问任何帖子并显示“保存”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19466374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档