Permitir subir archivos SVG

Permitir subir archivos SVG

Código que permite subir archivos SVG a la biblioteca de WordPress.

add_filter(
	'upload_mimes',
	function ( $upload_mimes ) {
		// Por defecto, sólo los usuarios administradores pueden añadir SVG.
		// Para permitir más tipos de usuarios, edita o comenta las líneas siguientes, pero ten cuidado con los riesgos de seguridad si permites que cualquier usuario cargue archivos SVG.
		if ( ! current_user_can( 'administrator' ) ) {
			return $upload_mimes;
		}

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

		return $upload_mimes;
	}
);


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
);

¿Te ha gustado este código? Coméntalo con códigosWP:

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Scroll al inicio