1

I'm not strong in WP programming, bu I try to understand what to do to enable SVG in my site. I found that in need to add function below to functions.php:

/**
 * Add SVG capabilities
 */
function wpcontent_svg_mime_type( $mimes = array() ) {
  $mimes['svg']  = 'image/svg+xml';
  $mimes['svgz'] = 'image/svg+xml';
  return $mimes;
}
add_filter( 'upload_mimes', 'wpcontent_svg_mime_type' );

wpcontent should be replaced with namespace. But how to know what is my WP namespace?

Add lines below to .htaccess file after the line, #End WordPress:

# Add SVG Mime Types
AddType image/svg+xml svg
AddType image/svg+xml svgz

Is anything else I shoud do?

UPD. After this procedure iI have following:

Now I can upload files to Media, but no preview. And when I do insert nothing happens - no new picture.

How to solve this?

3
  • Your namespace, or rather your prefix, is whatever you choose it to be. Don't call it a namespace though, namespaces are something different in PHP. E.g. I prefix my functions with tomjn_, but I could have prefixed with moomins_ or cup_of_tea_, so long as they're unique to my code it doesn't matter
    – Tom J Nowell
    Commented Oct 25, 2015 at 15:12
  • 1
    I would also argue your question has been answered, and that your preview and thumbnail issue should be a second/new question
    – Tom J Nowell
    Commented Oct 25, 2015 at 15:14
  • Relevant: wordpress.stackexchange.com/questions/157480/…
    – Tom J Nowell
    Commented Oct 25, 2015 at 15:15

2 Answers 2

1

Adding the following code to my functions.php works great for me. No need to modify the .htaccess file at all.

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

add_filter( 'upload_mimes', 'cc_mime_types' );

This allows me to upload and display SVG images without any issues.

3
  • Got error after adding add_filter. Fatal error: Call to undefined function add_filter() in /home/.../wp-includes/functions.php on line 32
    – vico
    Commented Oct 25, 2015 at 14:52
  • Where did you put the code? You shouldn't be modify WordPress core
    – Tom J Nowell
    Commented Oct 25, 2015 at 15:13
  • 1
    @vico As Tom pointed out, it looks like you've put the code in Wordpress core. When I say "functions.php", I'm referring to the current Theme's "functions.php" file.
    – RaajTram
    Commented Oct 25, 2015 at 15:15
1

But how to know what is my WP namespace?

namespacing a function, in WP land, just means prefixing it with something that makes it more unique and unlikely to conflict with anything else. Generally that means you do both a "vendor" and a "package". For instance, if I'm building a plugin, my function upload mimes function might be...

add_filter('upload_mimes', 'chrisguitarguy_pluginname_mimes');
function chrisguitarguy_pluginname_mimes($mimes)
{
    // ...
}

Where chrisguitarguy is my "vendor" name and pluginname is the package. You can do this PHP namespaces as well.

namespace Chrisguitarguy\PluginName;

add_action('upload_mimes', __NAMESPACE__.'\\mimes');
function mimes($mimes)
{
    // ...
}

Is anything else I shoud do?

Nope, what you have is perfect. You may want to specify that svgz is gzipped in your .htaccess.

AddType image/svg+xml .svg .svgz
// http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addencoding
AddEncoding gzip svgz

Newer versions of apache might include this config by default?

1
  • Now I can upload files to Media, but no preview. And when I do insert nothing happens - no new picture.
    – vico
    Commented Oct 25, 2015 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.