Skip to content

Instantly share code, notes, and snippets.

@webgurus
Created August 9, 2022 12:52
Show Gist options
  • Save webgurus/97dd46d6915046ee300be215e3400f37 to your computer and use it in GitHub Desktop.
Save webgurus/97dd46d6915046ee300be215e3400f37 to your computer and use it in GitHub Desktop.
Modify ACF oEmbed field to add attributes.
/**
* Add custom attributes to oEmbed iframe src
*
* @param string $iframe
* @return string
*/
public static function oEmbedIframe($iframe) {
// Use preg_match to find iframe src.
preg_match('/src="(.+?)"/', $iframe, $matches);
$src = $matches[1];
// Add extra parameters to src and replace HTML.
$params = array(
'showDescriptions' => 0,
'autoPlay' => 1,
'autoMute' => 1
);
$new_src = add_query_arg($params, $src);
$iframe = str_replace($src, $new_src, $iframe);
// Add extra attributes to iframe HTML.
$html = str_replace('frameborder="0"', 'frameborder="0" allow="accelerometer; autoplay; muted; encrypted-media; gyroscope; picture-in-picture"', $iframe);
// Display customized HTML.
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment