Skip Navigation

[Resolved] Restrict file types on image custom field

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Shane 3 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#2051931
Screenshot_2021-05-11 Edit Field Group ‹ Platinum Condo Deals — WordPress.png

Tell us what you are trying to do?
I want to restrict image file types on the custom field in one of our custom post type

Is there any documentation that you are following?
N/A

Is there a similar example that we can see?
similar functionality with acf image field, check uploaded image

What is the link to your site?
hidden link

#2052193

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Suhela,

Thank you for getting in touch.

Unfortunately no, there isn't a built in way to do this and the file type will need to be checked when it is being uploaded. You will need to use a hook similar to the on below.

add_filter('cred_form_validate', 'tsbmp1_validate_data_281_303',10,2);
function tsbmp1_validate_data_281_303($error_fields, $form_data){
     
     $form_ids = array( 271 );
     
    // field data are field values and errors
     list($fields,$errors)=$error_fields;
     
     if (in_array($form_data['id'],$form_ids) ) {
        if ( ( !empty( $fields[ 'wpcf-image' ])) ) {


            $file_type = $fields['wpcf-image']['file_data']['type'];

       
             
            if ( ( $file_type != 'image/jpeg' )  ) {
                $errors['image'] = 'Please ensure your image is a JPG/JPEG ;
            }
        }
    }
    return array($fields,$errors);
}

The code above code will only allow the file to be uploaded if the file is a JPEG so this restricts it to JPEG only.

Please let me know if this example helps.
Thanks,
Shane

#2053187

Hello Shane,

does this apply to the backend custom field? how do I get the form id of the custom field?

#2053223

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Suhela,

No this will only apply to the frontend custom fields in the Toolset Forms.

The form ID can be found by going to Toolset -> Post Forms from there you should see the ID of the form beside its name.

You will also need to replace wpcf-image with the slug of your image field keeping the wpcf- prefix.

Please let me know if this helps.
Thanks,
Shane

#2054299

Hello Shane,

Thank you very much for your help, unfortunately, I need the filtering on the backend when adding an image.

#2054321

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Suhela,

Unfortunately we don't have a hook that can do this so 3rd party plugin will be needed or the use of custom code is needed.

I manage to locate a plugin that should be able to help you resolve this.
hidden link

It essentially allows you to restrict the file type that can be uploaded to the wordpress media library.

Thanks,
Shane