Skip to content

Instantly share code, notes, and snippets.

@isaumya
Created September 26, 2016 20:08
Show Gist options
  • Save isaumya/c4590ac24d6e9f9b0235756fa02bfa7e to your computer and use it in GitHub Desktop.
Save isaumya/c4590ac24d6e9f9b0235756fa02bfa7e to your computer and use it in GitHub Desktop.
Showing image in WordPress works for both SVG & non SVG image
<?php
/*** Function to show images whether SVG or non SVG ***/
/*** $size & $attribute both can hold array if you want ***/
function show_image( $image_id, $size = null, $attributes = null ) {
//first lets get the file info sto understand what kind of file it is
//as for svg file we will take different approach
$file_info = pathinfo( wp_get_attachment_url( $image_id ) );
//so, if the file type is SVG
if ( $file_info['extension'] === 'svg' ) {
return file_get_contents( wp_get_attachment_url( $image_id ) );
} else {
//for any other type of images i.e. JPG, PNG, GIF
//we can just simply use the wp_get_attachment_image() stock function
return wp_get_attachment_image( $image_id, $size, false, $attributes );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment