set an id for the youtube iframe with wp_oembed_get in wordpress

My theme uses wp_oembed_get for embedding youtube videos by iframe.

<?php echo wp_oembed_get( get_post_meta( get_the_ID(), 'wpex_post_video_oembed', true ) ); ?>

is it possible to give the iframe an id? I need it so use the youtube api.

Read More

Thank you

Related posts

Leave a Reply

1 comment

  1. You can use the filter oembed_dataparse:

    add_filter( 'oembed_dataparse', function( $return, $data, $url ){
        if( false === strpos( $return,'youtube.com' ) )
            return $return;
    
        $id = explode( 'watch?v=', $url );
        $add_id = str_replace( 'allowfullscreen>', 'allowfullscreen id="yt-'.$id[1].'">', $return );
        return $add_id;
    }, 10, 3 );
    

    This is a snapshot of the received parameters (“result” is showing another filter, oembed_result, but dataparse seems more complete):