Excluding function on certain pages

Viewed 50

I want to prevent vendors on my WordPress website to upload low resolution images. I want to exclude certain pages as I want them to be able to let them upload their logo and banner. I already received following code snippet which works on all pages:

    function resize_image_resolution($file) {
    if(is_user_wcmp_vendor(get_current_user_id())){
        $image = getimagesize($file['tmp_name']);
            $minimum = array(
                'width' => '1080',   //set your minimum
                'height' => '1080'
            );
            $maximum = array(
                'width' => '12000',  //set your maximum
                'height' => '12000'
            );
            $image_width = $image[0];
            $image_height = $image[1];

            $too_small = "Image dimensions are too small.";
            $too_large = "Image dimensions are too large.";

            if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
                $file['error'] = $too_small; 
                return $file;
            }
            elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
                $file['error'] = $too_large; 
                return $file;
            }
            else
                return $file;
    } else {
            return $file;
    }
}

    add_filter('wp_handle_upload_prefilter', 'resize_image_resolution');

I also received following hook to exclude a certain page but unfortunately I'm not able to combine them. Anyone that can help me out?

do_action('wcmp_before_shop_front');
1 Answers

You would use a if statement. You can check for different things depending on what your end result is.

Hide function for specific page

    if ( !get_page( 24 ) ) { // ID of the page you don't want to run it on
    function resize_image_resolution($file) {
        if(is_user_wcmp_vendor(get_current_user_id())){
            $image = getimagesize($file['tmp_name']);
                $minimum = array(
                    'width' => '1080',   //set your minimum
                    'height' => '1080'
                );
                $maximum = array(
                    'width' => '12000',  //set your maximum
                    'height' => '12000'
                );
                $image_width = $image[0];
                $image_height = $image[1];
    
                $too_small = "Image dimensions are too small.";
                $too_large = "Image dimensions are too large.";
    
                if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
                    $file['error'] = $too_small; 
                    return $file;
                }
                elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
                    $file['error'] = $too_large; 
                    return $file;
                }
                else
                    return $file;
        } else {
                return $file;
        }
    }
    
    add_filter('wp_handle_upload_prefilter', 'resize_image_resolution');
}

Hide function for specific template

    if ( !is_page_template( 'about' ) ) { // name of template you don't want to run it on
    function resize_image_resolution($file) {
        if(is_user_wcmp_vendor(get_current_user_id())){
            $image = getimagesize($file['tmp_name']);
                $minimum = array(
                    'width' => '1080',   //set your minimum
                    'height' => '1080'
                );
                $maximum = array(
                    'width' => '12000',  //set your maximum
                    'height' => '12000'
                );
                $image_width = $image[0];
                $image_height = $image[1];
    
                $too_small = "Image dimensions are too small.";
                $too_large = "Image dimensions are too large.";
    
                if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
                    $file['error'] = $too_small; 
                    return $file;
                }
                elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
                    $file['error'] = $too_large; 
                    return $file;
                }
                else
                    return $file;
        } else {
                return $file;
        }
    }
    
    add_filter('wp_handle_upload_prefilter', 'resize_image_resolution');
}
Related