Skip to content

Instantly share code, notes, and snippets.

@jdevalk
Created January 5, 2012 12:29
Show Gist options
  • Save jdevalk/1565059 to your computer and use it in GitHub Desktop.
Save jdevalk/1565059 to your computer and use it in GitHub Desktop.
oembed function to use content_width appropriately
<?php
function yoast_oembed_dataparse( $html, $data, $url ) {
global $content_width;
preg_match( '/width="(\d+)"/', $html, $matches );
$width = $matches[1];
preg_match( '/height="(\d+)"/', $html, $matches );
$height = $matches[1];
$aspect_ratio = $width / $height;
$new_height = $content_width / $aspect_ratio;
$html = str_replace( 'width="'.$width.'"', 'width="'.$content_width.'"', $html );
$html = str_replace( 'height="'.$height.'"', 'height="'.$new_height.'"', $html );
return $html;
}
add_filter( 'oembed_dataparse', 'yoast_oembed_dataparse', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment