treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Allow SVG through WordPress Media Uploader

Last updated on:

For your functions.php file or a functionality plugin:

function cc_mime_types( $mimes ){
	$mimes['svg'] = 'image/svg+xml';
	return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );

Without this, SVG files will be rejected when attempting to upload them through the media uploader.

View Comments

Comments

  1. JamesonLewis3rd
    Permalink to comment#

    Thank you!

  2. Permalink to comment#

    Thanks alot. I have a Plugin for WordPress with these lines:

    _enable_svg_mime_type();
    }

    private function _enable_svg_mime_type() {
    add_filter( ‘upload_mimes’, array( &$this, ‘allow_svg_uploads’ ) );
    }

    public function allow_svg_uploads( $existing_mime_types = array() ) {
    return $this->_add_mime_type( $existing_mime_types );
    }

    private function _add_mime_type( $mime_types ) {
    $mime_types[ 'svg' ] = ‘image/svg+xml’;

    return $mime_types;
    }

    }

    if ( class_exists( ‘scalable_vector_graphics’ ) and ! isset( $scalable_vector_graphics ) ) {
    $scalable_vector_graphics = new scalable_vector_graphics();
    $scalable_vector_graphics->execute();
    }

    ?>

    Is this the same or does it more than your function?

  3. Karl
    Permalink to comment#

    Worked like a charm! Just built my first SVG based site with absolutely no image files using WordPress. You’ve been a great help. Thanks so much :)

  4. Steve
    Permalink to comment#

    Works brilliantly for the upload part, however, the SVG displays huge wherever it gets displayed in WordPress admin, is there a way to prevent this?

    • Steve
      Permalink to comment#

      Using another snippet I found on here, a simple style injection seems to fix the display of SVGs within the admin area, this fixes the display in the media uploader and the featured image area:

       function fix_svg() {
          echo '<style type="text/css">
                .attachment-266x266, .thumbnail img { 
                     width: 100% !important; 
                     height: auto !important; 
                }
                </style>';
       }
       add_action('admin_head', 'fix_svg');
      

Leave a Comment

Use markdown or basic HTML and be nice.