• I have uploaded some SVG files to my website however when I upload them to the media library the image is blank or it has the broken image thumbnail. I hear there may be some issue in the functions.php maybe but not sure. Any help would be greatly appreciated.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @maxranq

    Have you uploaded the SVG properly in your media library?

    I have a simple solution for you – Just copy and paste the following code in the functions.php file of your active theme. I always recommend putting the code in the child theme.

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

    Here is a simple video too – https://www.loom.com/share/35f11571300d4bf99c9a6246788aee4e?sid=64a71834-4620-47ad-888d-2524e4e057b3

    I hope this will solve your problem. After using the code, you can upload and add svg file anywhere inside your Website.

    Thank You

    Thread Starter maxranq

    (@maxranq)

    Hey @radaevich so I have the following code using the WPcode plugin. That SVG plugin breaks my site for some reason. I don’t have an issues with allowing SVG uploads but when the image is uploaded its just blank.

    /**
     * Allow SVG uploads for administrator users.
     *
     * @param array $upload_mimes Allowed mime types.
     *
     * @return mixed
     */
    add_filter(
    	'upload_mimes',
    	function ( $upload_mimes ) {
    		// By default, only administrator users are allowed to add SVGs.
    		// To enable more user types edit or comment the lines below but beware of
    		// the security risks if you allow any user to upload SVG files.
    		if ( ! current_user_can( 'administrator' ) ) {
    			return $upload_mimes;
    		}
    
    		$upload_mimes['svg']  = 'image/svg+xml';
    		$upload_mimes['svgz'] = 'image/svg+xml';
    
    		return $upload_mimes;
    	}
    );
    
    /**
     * Add SVG files mime check.
     *
     * @param array        $wp_check_filetype_and_ext Values for the extension, mime type, and corrected filename.
     * @param string       $file Full path to the file.
     * @param string       $filename The name of the file (may differ from $file due to $file being in a tmp directory).
     * @param string[]     $mimes Array of mime types keyed by their file extension regex.
     * @param string|false $real_mime The actual mime type or false if the type cannot be determined.
     */
    add_filter(
    	'wp_check_filetype_and_ext',
    	function ( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) {
    
    		if ( ! $wp_check_filetype_and_ext['type'] ) {
    
    			$check_filetype  = wp_check_filetype( $filename, $mimes );
    			$ext             = $check_filetype['ext'];
    			$type            = $check_filetype['type'];
    			$proper_filename = $filename;
    
    			if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
    				$ext  = false;
    				$type = false;
    			}
    
    			$wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' );
    		}
    
    		return $wp_check_filetype_and_ext;
    
    	},
    	10,
    	5
    );
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.