2

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

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; 
}
  • thanks for the help . but return array("error"=> print_r($_POST, true) ); is returning nothing .. to be more exact it returns array( [script] => true ) . If we can somehow get the name of the inout box then we can put the condition in tyour code .. This is an ajax form , can this is the reason it doesn't return anything? – aryan Sep 11 '16 at 13:56
  • Yeah, we need more information at this point. Please provide all of the form code. – Dave Romsey Sep 11 '16 at 17:22
  • Hey Dave i have updated the question . Please check it out . I hope it helps . thanks – aryan Sep 11 '16 at 18:57
  • Thanks for the updated information and code. I've updated the answer. I was able to successfully test for the job_logo upload. – Dave Romsey Sep 11 '16 at 19:27
  • 1
    Hey Dave thanks for the help .. that worked awesome!!! :) you have saved my life .. one- fourth times.. thanks... a lot – aryan Sep 11 '16 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.