wp_embed_register_handler not working

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="https://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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x