I want to set minimum image dimensions for specific input field with id= "job_logo" in a submission form on the front end

According to this answer
How to Require a Minimum Image Dimension for Uploading?

Following code sets the limit for all the image uploads

add_filter('wp_handle_upload_prefilter','tc_handle_upload_prefilter');
function tc_handle_upload_prefilter($file)
{

    $img=getimagesize($file['tmp_name']);
    $minimum = array('width' => '640', 'height' => '480');
    $width= $img[0];
    $height =$img[1];

    if ($width < $minimum['width'] )
        return array("error"=>"Image dimensions are too small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px");

    elseif ($height <  $minimum['height'])
        return array("error"=>"Image dimensions are too small. Minimum height is {$minimum['height']}px. Uploaded image height is $height px");
    else
        return $file; 
}

Is there a way i can do this for one specific field with id=”job_logo” i.e

Following is the class of wp job manager used for submission form

https://github.com/Automattic/WP-Job-Manager/blob/c838d6ee3a3d0fd224d666bfee55f58517e10cf6/includes/forms/class-wp-job-manager-form-submit-job.php#L206

this is the code i have used to add the upload image field

add_filter( 'submit_job_form_fields', 'my_custom_tax' );
function my_custom_tax( $fields ) {
  $fields['job']['job_logo'] = array(
          'label'       => __( 'Logo', 'wp-job-manager' ),
          'type'        => 'file',
          'required'    => false,
          'placeholder' => '',
          'priority'    => 1.15,
          'ajax'        => true,
          'multiple'    => false,
          'allowed_mime_types' => array(
            'jpg'  => 'image/jpeg',
            'jpeg' => 'image/jpeg',            
            'png'  => 'image/png'
          )
  );
  return $fields;
}

Following is the html output for upload logo image field

<input type="file" class="input-text wp-job-manager-file-upload" data-file_types="jpg|jpeg|png" name="job_logo" id="job_logo" placeholder="">

1 Answer
1

The $file array doesn’t have anything that you’d be able to use, but $GLOBALS has some helpful information. You can uncomment the debugging line that contains $GLOBALS to see what it contains.

I have added a guard clause to the top of the tc_handle_upload_prefilter function. This ensures that the file size checking code is only run when the job_logo file is being uploaded.

add_filter('wp_handle_upload_prefilter','tc_handle_upload_prefilter');
function tc_handle_upload_prefilter($file) {
    // Debugging...
    //return array("error"=> print_r( $_POST, true ) );
    //return array("error"=> print_r( $file, true ) );

    //return array("error"=> print_r( $GLOBALS, true ) ); // This is helpful...
    //return array( "error"=> print_r( $GLOBALS['job_manager_uploading_file'], true ) ); // Now we're cooking!

    if ( ! isset( $GLOBALS['job_manager_uploading_file'] ) || ( 'job_logo' !== $GLOBALS['job_manager_uploading_file'] ) ) {
        return $file;       
    }

    $img = getimagesize( $file['tmp_name'] );
    $minimum = array( 'width' => '640', 'height' => '480' );
    $width = $img[0];
    $height = $img[1];

    if ( $width < $minimum['width'] )
        return array( "error"=>"Image dimensions are too small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px");

    elseif ( $height < $minimum['height'] )
        return array("error"=>"Image dimensions are too small. Minimum height is {$minimum['height']}px. Uploaded image height is $height px");
    else
        return $file; 
}

Leave a Reply

Your email address will not be published. Required fields are marked *