• Attempting to unset the mp4 Mime so editors can not upload video files to the server. Have added this function to the child theme’s function file.

    function my_mime_types($mime_types){
    if (!current_user_can(‘editor’)) {
    return;
    }
    unset($mime_types[‘mp4’]); // Removes the .mp4 extension
    return $mime_types;
    }
    add_filter(‘upload_mimes’, ‘my_mime_types’, 1, 1);

    but editor’s can still upload mp4 files.

    if substitute pdf for mp4 like this

    function my_mime_types($mime_types){
    if (!current_user_can(‘editor’)) {
    return;
    }
    unset($mime_types[‘pdf’]); // Removes the .pdf extension
    return $mime_types;
    }
    add_filter(‘upload_mimes’, ‘my_mime_types’, 1, 1);

    function works and returns this error “Sorry, you are not allowed to upload this file type.”

    Any ideas as why the mp4 function does not work?

    • This topic was modified 2 years, 4 months ago by Jan Dembowski.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The mp4 mimetype is called “mp4|m4v”. So:

    unset($mime_types['mp4|m4v']);

    if (!current_user_can(‘editor’)) {

    https://developer.www.ads-software.com/reference/functions/current_user_can/

    While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

    What you probably want is only admin to be able to uploade mp4 – in which case check an admin capability like manage option

    also

    if (!current_user_can(‘editor’)) {
    return;
    }

    is wrong as it needs to return $mime_types

    However the mime type for mp4 is mp4|mv4 so your IF is wrong

    Also you may wish to filter all video mime types

    here is working code that disables all video mime types for non admins

    add_filter(
    	'upload_mimes',
    	function ( $mime_types ) {
    		if ( ! current_user_can( 'manage_options' ) ) {
    			// allow admins to upload mp3
    			return $mime_types;
    		}
    		//  rest of the world remove them
    		//  unset( $mime_types['mp4|m4v'] ); // Removes the .mp4 extension
    		//  would unset only  video/mp4
    		//  but below would remove all mime types starting with video
    		$mime_types = array_filter(
    			$mime_types,
    			function ($m) {
                    if (0 === strpos($m,'video')) {
    					return false;
                    }
    				return true;
    			}
    		);
    
    		return $mime_types;
    	},
    	1,
    	1
    );
    
    Thread Starter Safety13

    (@safety13)

    Thanks Alan. Your solution works perfectly!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Unset mp4 Mime’ is closed to new replies.