WP_EMBED_REGISTER_HANDLER不工作

时间:2016-09-06 作者:kmligue

我在这个链接中尝试了这个例子https://codex.wordpress.org/Function_Reference/wp_embed_register_handler 但它没有起作用。这是我的全部代码:

add_action(\'init\', function() {
    wp_embed_register_handler( \'forbes\', \'#http://(?:www|video)\\.forbes\\.com/(?:video/embed/embed\\.html|embedvideo/)\\?show=([\\d]+)&format=frame&height=([\\d]+)&width=([\\d]+)&video=(.+?)($|&)#i\', \'wp_embed_handler_forbes\' );
});

function wp_embed_handler_forbes( $matches, $attr, $url, $rawattr ) {

$embed = sprintf(
        \'<iframe src="http://www.forbes.com/video/embed/embed.html?show=%1$s&format=frame&height=%2$s&width=%3$s&video=%4$s&mode=render" width="%3$spx" height="%2$spx" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>\',
        esc_attr($matches[1]),
        esc_attr($matches[2]),
        esc_attr($matches[3]),
        esc_attr($matches[4])
        );

return apply_filters( \'embed_forbes\', $embed, $matches, $attr, $url, $rawattr );
}
你知道为什么这样不行吗?

2 个回复
最合适的回答,由SO网友:birgire 整理而成

我修改了你在法典中发布的示例:

/**
 * Embed support for Forbes videos
 *
 * Usage Example:
 *
 *     http://www.forbes.com/video/5049647995001/
 */
add_action( \'init\', function()
{
    wp_embed_register_handler( 
        \'forbes\', 
        \'#http://www\\.forbes\\.com/video/([\\d]+)/?#i\', 
        \'wp_embed_handler_forbes\' 
    );

} );

function wp_embed_handler_forbes( $matches, $attr, $url, $rawattr )
{
    $embed = sprintf(
        \'<iframe class="forbes-video" src="https://players.brightcove.net/2097119709001/598f142b-5fda-4057-8ece-b03c43222b3f_default/index.html?videoId=%1$s" width="600" height="400" frameborder="0" scrolling="no"></iframe>\',
        esc_attr( $matches[1] ) 
     );

    return apply_filters( \'embed_forbes\', $embed, $matches, $attr, $url, $rawattr );
}
目前iframe 具有固定的高度和宽度。

您可以根据自己的需要进行调整,例如使用主题$content_width 或者直接从粘贴的视频url传递高度/宽度信息。

Update: 我添加了一个warning to the Codex page, 直到发布更好的示例。

SO网友:mosne

这里是linkedin的联合国示例

/** linkedin embed
*  example : https://www.linkedin.com/embed/feed/update/urn:li:share:6704370979721777152
*/
add_action(
    \'init\',
    function () {
        wp_embed_register_handler(
            \'linkedin\',
            \'#https://www\\.linkedin\\.com/embed/feed/update/urn:li:share:([\\d]+)/?#i\',
            \'embed_handler_linkedin\'
        );
    }
);

function embed_handler_linkedin( $matches, $attr, $url, $rawattr ) {

    $embed = sprintf(
        \'<iframe src="https://www.linkedin.com/embed/feed/update/urn:li:share:%s" width="500" height="453" frameborder="0" scrolling="no" class="block__linkedin"></iframe>\',
        esc_attr( $matches[1] )
    );

    return apply_filters( \'embed_linkedin\', $embed, $matches, $attr, $url, $rawattr );
}

相关推荐

Embed Form In Header

我正在使用WordPress主题Twenty Ninteen,并试图借助Travelpayouts提供的旅游联盟API创建一个旅游博客。<script charset=\"utf-8\" src=\"//www.travelpayouts.com/widgets/920a6b8c6fc20070013d2e04f690210d.js?v=1649\" async></script> 这是要在页面/帖子上显示的表单代码,如果我在任何帖子上使用的页面代码工作正常,但我需要在标