Skip to content

Instantly share code, notes, and snippets.

@zzap
Created August 27, 2024 15:56
Show Gist options
  • Save zzap/827c34cf84c5dfef0230a3315805fe3b to your computer and use it in GitHub Desktop.
Save zzap/827c34cf84c5dfef0230a3315805fe3b to your computer and use it in GitHub Desktop.
Set aspect ratio for the iframe with WP_HTML_Tag_Processor
<?php
/**
* Set aspect ratio for the iframe.
*/
$tags = new WP_HTML_Tag_Processor( $html );
if ( $tags->next_tag( 'iframe' ) ) {
$width = $tags->get_attribute( 'width' );
$height = $tags->get_attribute( 'height' );
$style = $tags->get_attribute( 'style' );
$tags->set_attribute( 'width', '100%' );
$tags->remove_attribute( 'height' );
$tags->set_attribute( 'style', $style . "; aspect-ratio: {$width}/{$height};" );
}
echo $tags->get_updated_html();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment