简体   繁体   中英

How to use SVG for logo in storefront theme

I want to use a SVG logo in my woocommerce storefront theme. I can't use the Customizer b/c it won't allow SVG uploads. Is there a way around that restriction?

If not, how would I edit the hook/action to use the SVG?

I see the storefront_site_branding template function in the storefront/inc folder and I tried creating an inc folder in the child theme but apparently the override does not work on files within the inc folder.

If you are using a child-theme you can add this to your functions.php, if you update you themes functions.php it may override during your next patch/update.

I sourced this code from: https://themeisle.com/blog/add-svg-to-wordpress/

Method 1: Use the SVG Support plugin If you're looking for the fastest way to add SVG to WordPress, this is it. We're going to use the SVG Support plugin, which enables this particular image format and adds support for it to your media library:

The process is simple. You just need to install and activate the plugin as usual, and then you'll be able to add SVGs to your WordPress site.

WordPress now requires us to have the tag in our SVG files before uploading. Please open your SVG file in any code editor (such as sublime text) and add the following to the very first line of your SVG file and save, so that you don't encounter security errors:

<?xml version="1.0" encoding="utf-8"?>

However, there are two more settings you might want to change depending on your needs. First off, let's go to the Settings → SVG Support tab:

How to add SVG to WordPress using SVG Support plugin

Inside, you'll find two options. The first turns on the plugin's Advanced Mode, which lets you target your SVGs with CSS. If you don't want to animate your SVGs, then you can skip this option.

Second, you can also restrict the ability to upload SVGs to administrators only by enabling the Restrict to Administrators? feature. That one's up to you!

Method 2: Modify your site's functions.php file Every WordPress website has its own functions.php file. This essential component includes important functions, classes, and filters. It's also your ticket to adding SVG support to WordPress through a few lines of code.

To reach this file, you'll need to access your website via FTP. If you don't have a client, we recommend using FileZilla. Once you've found your FTP credentials and accessed your site, you'll want to head to your root folder, which is usually either called public_html or named after your site:

The WordPress root folder.

Now, enter the wp-includes folder and look for the functions.php file within. It's important to note that this is the parent file, while there are also individual functions.php files for each of your themes:

The wp-includes folder.

For this example, we'll add the code to the parent file. However, you may find the changes are lost when WordPress is updated, so feel free to alternatively add it to your theme-specific functions.php file depending on the approach you're more comfortable with.

(Editor's note: Doing this in your theme's functions file is actually the recommended approach.)

Access the functions.php file now by right-clicking on it and choosing the View/Edit option. This will open it using your default text editor. Now, scroll to the bottom and paste this code snippet there:

function add_file_types_to_uploads($file_types){
$new_filetypes = array();
$new_filetypes['svg'] = 'image/svg+xml';
$file_types = array_merge($file_types, $new_filetypes );
return $file_types;
}
add_filter('upload_mimes', 'add_file_types_to_uploads');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM