phpwordpressiframequery-stringoembed

Add extra query string to src attribute's value of an iframe element that has been generated or outputted by wp_oembed_get() function


I want to add an extra query string to the src attribute's value of the iframe element. I want to achieve this by using PHP or WordPress and not by JavaScript.

The thing is that I want to use Youtube iFrame API, which is only possible if the iframe has a query string in which enablejspai=1 src="https://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1"

Currently, the user will copy-paste the video URL and my code will grab that URL and will convert it to the iframe element.

$embed_code = wp_oembed_get( $video_post_url);
echo $embed_code;

The above code will output an iframe element with src attribute with value of just plain url but I want to add a query string to it and make it plain-url?enablejsapi=1 So I can use Youtube Iframe API.


Solution

  • You can get the url as follows:

    // Load value.
    $iframe = wp_oembed_get( $video_post_url );
    
    // Use preg_match to find iframe src.
    preg_match('/src="(.+?)"/', $iframe, $matches);
    
    // Iframe URL
    $src = $matches[1];