How To Customize WordPress Image Upload Error Message

Question

I have recently added a restriction on my WordPress theme for uploading images, images above 100kb are not allowed, and the code for it worked. And now I can see this “wp2146012.jpg: This file exceeds the maximum upload size for this site.” However, I have tried manipulating this error message, but I saw that wp_handle_upload_prefilter has been converted to a dynamic hook, I’m new to coding so I have tried to do two things with code, but none seem to have worked. And now I’m stuck, here are the 2 codes I tried running to manipulate this.

function upload_size_error( $file ) {
    if ( current_user_can( 'manage_options' ) ) {
        if ( $file['size'] > $limit ) {
            $file['error'] = __( 'Maximum upload size is 100kb', 'wp-job-manager' );
        }
    }
    return $file;
}

2nd Try Code

if ( ! function_exists( 'wp_handle_upload_error' ) ) {
    function wp_handle_upload_error( &$file, $message ) {
        $message = "Error: Maximum upload size is 100kb"
        return array( 'error'=>$message );
    }
}

And here is the code I used to limit the size, in case it is needed

function upload_size_error( $file ) {
    if ( current_user_can( 'manage_options' ) ) {
        if ( $file['size'] > $limit ) {
            $file['error'] = __( 'Maximum upload size is 100kb', 'wp-job-manager' );
        }
    }
    return $file;
}

add_filter( "{$action}_prefilter", 'upload_size_error' );
0
AcidBurn 9 months 0 Answers 81 views 0

Leave an answer