Skip to content

Instantly share code, notes, and snippets.

@yuriitaran
Created February 12, 2019 16:11
Show Gist options
  • Save yuriitaran/aa971217fe97f3efb60b829ae0241bdd to your computer and use it in GitHub Desktop.
Save yuriitaran/aa971217fe97f3efb60b829ae0241bdd to your computer and use it in GitHub Desktop.
Wordpress SVG support
/*
* Add SVG to allowed file uploads
*/
add_filter( 'upload_mimes', function ( $mimes = array() ) {
// allow SVG file upload
$mimes['svg'] = 'image/svg+xml';
$mimes['svgz'] = 'image/svg+xml';
return $mimes;
});
/**
* ADD ABILITY TO VIEW THUMBNAILS IN WP 4.0+
*/
add_action('admin_init', function () {
ob_start();
add_action('shutdown', function () {
$final = '';
$ob_levels = count(ob_get_level());
for ($i = 0; $i < $ob_levels; $i++) {
$final .= ob_get_clean();
}
echo apply_filters('final_output', $final);
}, 0);
add_filter('final_output', function ($content) {
$content = str_replace('<# } else if ( \'image\' === data.type && data.sizes && data.sizes.full ) { #>', '<# } else if ( \'svg+xml\' === data.subtype ) { #>
<img class="details-image" src="{{ data.url }}" draggable="false" />
<# } else if ( \'image\' === data.type && data.sizes && data.sizes.full ) { #>',
$content);
$content = str_replace('<# } else if ( \'image\' === data.type && data.sizes ) { #>', '<# } else if ( \'svg+xml\' === data.subtype ) { #>
<div class="centered">
<img src="{{ data.url }}" class="thumbnail" draggable="false" />
</div>
<# } else if ( \'image\' === data.type && data.sizes ) { #>',
$content);
return $content;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment