code a plugin that enable upload of .webp and .svg in medias library

Home » Snippets » code a plugin that enable upload of .webp and .svg in medias library
0

Created with:

Visibility: 

public

Creator: Aurélien

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: Custom Media Uploads
 * Description: Enables upload of .webp and .svg files in the media library.
 * Version: 1.0.0
 * Author: Your Name
 * License: GPL2+
 * Text Domain: wpturbo
 */

defined( 'ABSPATH' ) || exit;

/**
 * Allow .webp and .svg file uploads in media library.
 *
 * @param array $mimes Allowed mime types.
 * @return array Updated list of allowed mime types.
 */
function wpturbo_allow_webp_svg_uploads( $mimes ) {
    // Add .webp and .svg to the list of allowed mime types.
    $mimes['webp'] = 'image/webp';
    $mimes['svg']  = 'image/svg+xml';

    return $mimes;
}
add_filter( 'upload_mimes', 'wpturbo_allow_webp_svg_uploads' );

/**
 * Display .webp and .svg files in media library.
 *
 * @param array $types Associative array of allowed file types.
 * @return array Updated list of allowed file types.
 */
function wpturbo_display_webp_svg_in_library( $types ) {
    // Add .webp and .svg to the list of allowed file types.
    $types['webp'] = 'image';
    $types['svg']  = 'image';

    return $types;
}
add_filter( 'post_mime_types', 'wpturbo_display_webp_svg_in_library' );
				

Explanation:

This code creates a WordPress plugin that allows the upload of .webp and .svg files in the media library. The plugin consists of two functions hooked into WordPress filters.

The first function, wpturbo_allow_webp_svg_uploads, is hooked into the upload_mimes filter. It adds the .webp and .svg file extensions to the list of allowed mime types. This ensures that WordPress recognizes these file types as valid uploads.

The second function, wpturbo_display_webp_svg_in_library, is hooked into the post_mime_types filter. It adds the .webp and .svg file types to the list of allowed file types. By associating these file types with the 'image' category, WordPress will display them in the media library.

Remember to save this code in a file named 'wp-plugin.php' inside a folder named 'custom-media-uploads' under the 'plugins' directory of your WordPress installation. Activate the plugin from the WordPress dashboard, and you will be able to upload .webp and .svg files in the media library.

Register an account to save your snippets or go Pro to get more features.