Skip to content

Instantly share code, notes, and snippets.

@tripflex
Last active November 6, 2020 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/55d88547d73ac38a93c3d7530afd6cf1 to your computer and use it in GitHub Desktop.
Save tripflex/55d88547d73ac38a93c3d7530afd6cf1 to your computer and use it in GitHub Desktop.
Minimum file dimensions when using WP Job Manager Field Editor
<?php
add_filter( 'job_manager_upload_file_pre_upload', 'smyles_check_min_upload_dimensions', 9999, 3 );
/**
* Check Min Upload Dimensions
*
*
* @param array $file Array of $_FILE data to upload.
* @param array $args Optional file arguments
* @param array $allowed_mime_types Array of allowed mime types from field config or defaults
*
* @return mixed
*
*/
function smyles_check_min_upload_dimensions( $file, $args, $allowed_mime_types ) {
$min_width = 620;
$min_height = 480;
if ( ! is_array( $file ) || ! array_key_exists( 'tmp_name', $file ) || empty( $file['tmp_name'] ) ) {
return $file;
}
// Return $file when mime type fails, to allow core WP Job Manager to return mime type error
if ( array_key_exists( 'type', $file ) && ! in_array( $file['type'], $allowed_mime_types ) ) {
return $file;
}
if( $args['file_key'] !== 'featured_image' ){
return $file;
}
// Will return 0 or false if file is not an image, or unable to parse file uploaded
$imagesize = getimagesize( $file['tmp_name'] );
if ( empty( $imagesize ) || ! isset( $imagesize[0], $imagesize[1] ) ) {
return $file;
}
$width = $imagesize[0];
$height = $imagesize[1];
$min_dimensions = $min_width && $min_height ? sprintf( __( 'Minimum image dimensions are %1$s x %2$s (in pixels)' ), $min_width, $min_height ) : '';
if ( $min_width && ! empty( $width ) && ( $width < $min_width ) ) {
return new WP_Error( 'validation-error', sprintf( __( 'The minimum allowed image width for %1$s is %2$s pixels, the one you uploaded is %3$s pixels. %4$s' ), esc_attr( $file['name'] ), $min_width, esc_attr( $width ), $min_dimensions ) );
}
if ( $min_height && ! empty( $height ) && ( $height < $min_height ) ) {
return new WP_Error( 'validation-error', sprintf( __( 'The minimum allowed image height for %1$s is %2$s pixels, the one you uploaded is %3$s pixels. %4$s' ), esc_attr( $file['name'] ), (int) $field_config['min_upload_height'], esc_attr( $height ), $min_dimensions ) );
}
return $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment