Skip to content
Advertisement

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.

Advertisement

Answer

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];
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement