Skip to content

Instantly share code, notes, and snippets.

@schlessera
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schlessera/d9de7fa35bc6c693dfe8 to your computer and use it in GitHub Desktop.
Save schlessera/d9de7fa35bc6c693dfe8 to your computer and use it in GitHub Desktop.
Adjust the HTML that WordPress generates for SVG so they are resized to fit their container
<?php
/**
* Replace WordPress standard SVG width of 1 with 100%
* @author Alain Schlesser (alain.schlesser@gmail.com)
*
* By default, WordPress renders all SVG files uploaded through the Media
* Uploader with both width and height at "1".
*
* @param string $output HTML output that Genesis has generated for a
* requested image
* @return string modified version of the HTML $output
*
* @see http://docs.garyjones.co.uk/genesis/2.0.0/function-genesis_get_image.html
* @see http://www.w3.org/TR/SVG/mimereg.html
*/
function as_avoid_tiny_svgs( $output ) {
// replace width of "1" with a new width of "100%" and height of "1" with a
// new height of "auto"
$output = str_replace(
'width="1" height="1"',
'width="100%" height="auto"',
$output );
return $output;
}
add_filter( 'genesis_get_image', 'as_avoid_tiny_svgs' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment