How to Allow SVG Uploads in WordPress with Code

Introduction

WordPress is a popular content management system that allows users to create and manage websites with ease. By default, WordPress restricts certain file types from being uploaded for security reasons. However, there may be instances where you want to allow specific file types, such as SVG (Scalable Vector Graphics), to be uploaded by administrator users. In this guide, we will explain how to use a code snippet to enable SVG uploads for administrator users in WordPress.

Prerequisites

Before proceeding with the steps outlined in this guide, you should have the following:

  • A WordPress website
  • Administrator access to the WordPress dashboard

The Code Snippet

The code snippet provided below utilizes the upload_mimes filter to modify the allowed mime types for file uploads in WordPress. It specifically allows SVG uploads for administrator users.

PHP
/**
 * Allow SVG uploads for administrator users.
 *
 * @param array $upload_mimes Allowed mime types.
 *
 * @return mixed
 *
 * @author Wendy from staffup.ai
 */
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;
} );

Explanation

The code snippet provided above allows SVG uploads for administrator users in WordPress. Let’s break down how it works:

  1. The add_filter function hooks into the upload_mimes filter, which allows us to modify the allowed mime types for file uploads.
  2. The anonymous function passed as the second argument to add_filter takes the $upload_mimes array as a parameter. This array holds the currently allowed mime types.
  3. The code checks if the current user is not an administrator. If the condition is true, the original $upload_mimes array is returned without any modifications. This ensures that only administrator users can upload SVG files by default.
  4. If the current user is an administrator, the code adds the svg and svgz mime types to the $upload_mimes array. The corresponding MIME type for both file types is image/svg+xml.
  5. Finally, the modified $upload_mimes array is returned, which now includes the SVG mime types.

Customization Tips

The provided code snippet can be customized to suit different use-cases. Here are a few examples:

Allowing SVG Uploads for Other User Roles

If you want to allow SVG uploads for user roles other than administrators, you can modify the condition in the code. Instead of checking for the administrator role, you can replace it with the desired user role. For example, to allow SVG uploads for editors, you can use the following condition:

if ( ! current_user_can( 'editor' ) ) {
    return $upload_mimes;
}

Allowing Additional File Types

If you want to allow additional file types along with SVG, you can add them to the $upload_mimes array using the same format. For example, to allow both SVG and PDF uploads, you can modify the code as follows:

$upload_mimes['svg']  = 'image/svg+xml';
$upload_mimes['svgz'] = 'image/svg+xml';
$upload_mimes['pdf']  = 'application/pdf';

Conclusion

By following the steps outlined in this guide, you can easily enable SVG uploads for administrator users in WordPress. However, it’s important to note that allowing SVG uploads can pose security risks. Therefore, it’s crucial to consider the implications and take necessary precautions to ensure the safety of your website.

Customize Code

Try an AI WordPress Developer for Free

Do you need your code snippets to do something specific? Don’t worry you can use AI.


Email Icon

Get WP snippets in your inbox

Be one of the first to know about new PrimaryWP updates & code snippets for WordPress.

Newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *