I've been trying to understand a few errors I've got after implementing code I picked up online and modifying it to make the Wordpress [video] shortcode responsive, but I don't really know what to change to fix it after trying a bunch of stuff... These are the errors:
Warning: Illegal string offset 'height' in...on line 41
Warning: A non-numeric value encountered in...on line 41
Warning: Division by zero in...on line 41
And this is part of the the code creating the errors it seems:
$padding = ($meta['height']/$meta['width'])*100 - 25;
Can anyone help assist me in understanding and fixing these errors? This is the code in its entirety:
// OVERRIDE [VIDEO] SHORTCODE TO MAKE RESPONSIVE
// Thanks to: https://www.stirtingale.com/guides/2018/11/wordpress-video
function lookupIDfromURL($image_url) {
// basic lookup from DB to match media URL with media URL
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}
add_filter( 'wp_video_shortcode', function( $output ) {
// get SRC
// this is a bit hacky
preg_match( '@src="([^"]+)"@' , $output, $match );
$src = array_pop($match);
$src = preg_replace('/\?.*/', '', $src);
// get ID
$postid = lookupIDfromURL( $src );
$meta = wp_get_attachment_metadata($postid);
// let it autoplay
// and include playsinline to fix issues on iOS
$output = str_replace( "<video", "<video playsinline autoplay muted loop ", $output );
$output = str_replace( "controls=", "data-controls=", $output );
// wrap it all up
$str = preg_replace('/\<[\/]{0,1}div[^\>]*\>/i', '', $output);
$padding = ($meta['height']/$meta['width'])*100 - 25; // modified to include narrower height
$wrap = "<div class='embed-responsive' style='padding-bottom:". $padding ."%'>".$str."</div>";
$output = $wrap;
return $output;
} );