0

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;

} );
Community
  • 1
  • 1
Osu
  • 1,137
  • 2
  • 18
  • 34
  • 1
    `var_dump($meta);` – AbraCadaver May 17 '19 at 15:43
  • Comes back with: `string(0) ""`, so I guess there's nothing in $meta? Weird, as this was working fine on http until I install an SSL certificate on the domain and starting serving pages over https... might that have something to do with it? – Osu May 17 '19 at 15:47
  • Not a duplicate question, but the answer was to do with not checking if $meta had a value. Here's the answer for those of you stuck in the same situation: `// wrap it all up $str = preg_replace('/\<[\/]{0,1}div[^\>]*\>/i', '', $output); if(!empty($meta)) { $padding = ($meta['height']/$meta['width'])*100 - 25; // modified to include narrower height $wrap = "
    ".$str."
    "; } else { $wrap = "
    ".$str."
    "; } `
    – Osu May 17 '19 at 15:52

0 Answers0