I am attempting to use an SVG file for the logo of a site in WordPress, as you can see from the code below I have tried this be calling in the .svg file. Unfortunately, I can not get this to work…
//* Add support for custom header
add_theme_support( 'custom-header', array(
'header_image' => '/images/tgoc.svg',
'header-selector' => '.site-title a',
'header-text' => false,
'height' => 110,
'width' => 320,
) );
I have also actioned the following in funtions.php:
//* Enable SVG file upload
function custom_mtypes( $m ){
$m['svg'] = 'image/svg+xml';
$m['svgz'] = 'image/svg+xml';
return $m;
}
add_filter( 'upload_mimes', 'custom_mtypes' );
I know you can also insert the svg code, but my main question is where to insert this code? I would like to try and use Modernizr as well for back up.
Here is the CSS:
/* Logo, hide text */
.header-image .title-area {
padding: 0;
width: 340px;
height: 340px;
background: url(/images/logo.png);
display: block;
}
.no-svg .header-image {
// fallback
background-image: url(/images/logo.png);
}
.header-image .site-title a {
float: left;
min-height: 110px;
width: 100%;
}
First install the SVG Support plugin (and restrict it’s use to adminstrators for security reasons). Then you’ll encounter an error after uploading SVGs in which the 2nd step wants you to crop the image, but you can’t do that for SVGs. So do this:
to provide a button to “skip cropping” on the 2nd screen after selecting your svg image, which must be already cropped as the exact size you need.
All you need to do is take that array you defined above for custom header support, where you define the height and width, then add this:
so for my own custom theme the full snippet is:
which will provide you with a screen like this:
I’ve found that sometimes if you just click onto “skip cropping” and you’ve already had an image designated there before, you may encounter the site freezing. re-implementing svg header on dev site. To fix this, it’s necessary to remove the pre-existing header image, save that change. Exit the customizer. Then go in and reapply the image, clicking “skip cropping” for it to not freeze.
As this question is a year old at this point, I hope this is useful to someone else.
For your
functions.php
file or a functionality plugin:Without this, SVG files will be rejected when attempting to upload them through the media uploader.
References:-
https://css-tricks.com/snippets/wordpress/allow-svg-through-wordpress-media-uploader/
https://www.sitepoint.com/wordpress-svg/
The
custom-header
support seems to be one thing (Customizer -> Header Image) andcustom-logo
another. @Gray Ayer’s solution is great, but for the logo, which is set through Customizer -> Site Identity -> Logo and added withthe_custom_logo();
in header.php, you should have this in your functions.php: