1

I am trying to force minimum width and height requirements on featured image upload on a particular post type only. My minimum width/height code is ok, as it works just fine on all post types. I am having trouble getting it to run on JUST the "device" post type edit screen.

Pseudo Code: If is post-edit screen for "device" custom post type, run this filter

add_action( 'current_screen', 'bb_check_if_is_cpt_device' );
function bb_check_if_is_cpt_device() {
    $currentScreen = get_current_screen();
    if( $currentScreen->post_type == 'device' ) {
        add_filter( 'wp_handle_upload_prefilter','bb_check_image_sizes' );
    }
}
function bb_check_image_sizes( $file ) {
    $img = getimagesize($file['tmp_name']);
    $minimum = array('width' => '585', 'height' => '1024');
    $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;
}

Your Answer

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

Browse other questions tagged or ask your own question.