12

I'm working on a super-strict setting for adding images to Custom Fields in the Post Edit screen. My optimal scenario would be to add a a Custom Error message when the user tries to upload an image of the wrong size for that specific custom value.

I'm aware I can do this with any custom uploader, but I'd really prefer to do it with the regular Media Uploader. I'm also aware of the wp_handle_upload_prefilter which I already use to validate filenames and generate custom error messages based on generic requirements. What I need right now is a way to use custom requirements to reject uploads based on which field we're uploading to.

I'm also aware of Differentiate Featured Image from Post Images upon Upload but I wanted a pre-save solution.

This would look something like this:

enter image description here

Any ideas on how to go about informing wp_handle_upload_prefilter ( or a similar ) of which field we're dealing with?

  • Interesting question. I'm thinking, generally speaking, pass a custom variable via your custom media uploader modal script which you then can pass-trough for further differentiation along the way. This might be the only way, although I'm not a 100% sure about that. You will find the information you need about that, if you go on and look for it. Afterwards come back and complete your question with that concrete information or, if you solve it yourself, post your solution as answer. Good luck! – Nicolai Aug 4 '14 at 18:57
  • If I say custom I mean custom usage of the WordPress Media Uploader, like for example discussed here. – Nicolai Aug 4 '14 at 19:08
  • Yeah, I've done something like this before, in the days of the Flash Uploader. I was going for a cleaner way this time, but it just might not be possible. Will look into that, thanks! – moraleida Aug 4 '14 at 19:10
  • I hear you, but I actually don't think it is that bad of a way to do it. Besides, like I said, I just don't see/know another way to do it, but there might come someone along with more knowledge to clear that up. – Nicolai Aug 4 '14 at 19:28
  • 1
    Are you getting ahead with this? If not, but you have some code you are working on, post it. I'm willing to setup a bounty to the question if there is more foundation to it. – Nicolai Aug 6 '14 at 14:05
3

In your handler, if you set 'error', the error message will be displayed and will cancel the upload

add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' );

function custom_upload_filter( $file ) {
    $image_info   = getimagesize( $file['tmp_name'] );
    $image_width  = $image_info[0];
    $image_height = $image_info[1];

    if ( $image_with !== 800 || $image_height !== 600 ) {
        $file['error'] = __( 'Images must be sized exactly 800 * 600', 'your_textdomain' );
    }
    return $file;
}

If your user attempts to upload a different size, the message will be:

“thefile.png” has failed to upload due to an error  
Size must be exactly 800 * 600

Note that wp_handle_upload_prefilter comes very early in upload processing, so you may want to test if the file has been properly uploaded (from HTTP standpoint) and is an image before testing the size.

Ref: funtion wp_handle_upload() in the core file wp-admin/includes/file.php

  • Yes, this is what i am already doing to validate filenames and svg properties. What I need right now is a way to pass another parameter to that ( or another ) filter, indicating which field is being populated by that upload so that I can apply different restrictions for different fields. This is actually on hold right now. I'll update the question later on with my findings. – moraleida Aug 17 '14 at 17:48

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.