How can I change what can be uploaded?

WordPress Business Themes Forums Responsive Themes Dating Theme How can I change what can be uploaded? Reply To: How can I change what can be uploaded?

WordPress Business Themes Forums Responsive Themes Dating Theme How can I change what can be uploaded? Reply To: How can I change what can be uploaded?

  • JamesD
    JamesD
    Super Guru 1,296 points

    Hello Robert,

    I prefer to have some control of the size and type of files uploaded. IE… don’t want video uploaded. How can I make that change? Will it need to be changed in the code?

    Place the following near the bottom of your ‘_functions.php’ file:

    add_filter('wp_handle_upload_prefilter', 'custom_media_filter' );
    function custom_media_filter( $file ){
        // Filter set of allowed file extensions
        $aAllowedFiles = [ 'jpg', 'gif', 'png' ];
    
        // Get file extension
        $ext = pathinfo( $file['name'], PATHINFO_EXTENSION );
    
        // Verify file extension
        if ( !in_array($ext, $aAllowedFiles) ) {
            // Message to display if not a valid file.
            $file['error'] = __( 'Sorry, We do not accept this type of file.' );
        }
        return $file;
    }

    Note: The line $aAllowedFiles = [ 'jpg', 'gif', 'png' ]; is where you’ll place the file extensions of the types authorized to be uploaded. For dimension settings, take a look under Settings > Media

    Would using a child theme make it any easier?

    I don’t believe so.

    February 15, 2019 at 9:15 pm
  • Didn't find what you were after?

    Chat with us now

    or contact our support team