4

I tried the example in this link https://codex.wordpress.org/Function_Reference/wp_embed_register_handler but it didn't work. This is my whole code:

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 );
}

Any ideas why this is not working?

3
  • What url are you testing?
    – birgire
    Sep 6, 2016 at 9:02
  • this one http://www.forbes.com/video/5049647995001/
    – kmligue
    Sep 6, 2016 at 9:41
  • that's far away from the accepted url structure in the handler, I guess the Forbes embed service has changed since this example was added.
    – birgire
    Sep 6, 2016 at 10:10

2 Answers 2

2

I modified the example you posted from the Codex:

/**
 * 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 );
}

Currently the iframe has a fixed height and width.

You can hopefully adjust it to your needs, e.g. using the theme's $content_width or pass on the height/width information directly from the pasted video url.

Update: I added a warning to the Codex page, until a better example is posted.

2
  • Thanks! Code works on post editor. I can see embed in blog post. Currently im using buddypress on my site. When posting the link in activity page it didn't work. According to buddypress(codex.buddypress.org/getting-started/embeds) they are using the native wp embeds functionality. Could this be a buddypress issue?
    – kmligue
    Sep 7, 2016 at 1:57
  • Glad to hear it worked. It's been a while since I last used BuddyPress, but if it's working in the post content editor but not in activity pages then it sounds like an issue for BuddyPress. @KelvinMartinLigue
    – birgire
    Sep 7, 2016 at 8:06
0

here un example with 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 );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.