Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

I use a functiona function in my theme that prevents upload of big size image files (thanks to @fischi), but I want to apply it strictly to .jpg/.jpeg files only, because it prevents to upload .pdf files too. How to adapt this function?

I use a function in my theme that prevents upload of big size image files (thanks to @fischi), but I want to apply it strictly to .jpg/.jpeg files only, because it prevents to upload .pdf files too. How to adapt this function?

I use a function in my theme that prevents upload of big size image files (thanks to @fischi), but I want to apply it strictly to .jpg/.jpeg files only, because it prevents to upload .pdf files too. How to adapt this function?

Bumped by Community user
Bumped by Community user
Bumped by Community user
added 52 characters in body
Source Link
Yuri
  • 1.1k
  • 4
  • 25
  • 46
add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
function f711_image_size_prevent($file) {

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    if ( $type == $imagetype ) {

        // get filesize of upload
        $size = $file['size'];
        $size = $size / 1024; // Calculate down to KB

        // set sizelimit
        $limit = 700; // Your Filesize in KB

        // set imagelimit
        $imagelimit = 7;

        // query how many images the current user already uploaded
        global $current_user;
        $args = array(
                'orderby'         => 'post_date',
                'order'           => 'DESC',
                'numberposts'     => -1,
                'post_type'       => 'attachment',
                'author'          => $current_user->ID,
        );
        $attachmentsbyuser = get_posts( $args );

        if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
            $file['error'] = 'Image files must be smaller than '.$limit.'KB';
        } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
            $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
        }
        return $file;
    } else { return $file; }
 
}
add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
function f711_image_size_prevent($file) {

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    if ( $type == $imagetype ) {

        // get filesize of upload
        $size = $file['size'];
        $size = $size / 1024; // Calculate down to KB

        // set sizelimit
        $limit = 700; // Your Filesize in KB

        // set imagelimit
        $imagelimit = 7;

        // query how many images the current user already uploaded
        global $current_user;
        $args = array(
                'orderby'         => 'post_date',
                'order'           => 'DESC',
                'numberposts'     => -1,
                'post_type'       => 'attachment',
                'author'          => $current_user->ID,
        );
        $attachmentsbyuser = get_posts( $args );

        if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
            $file['error'] = 'Image files must be smaller than '.$limit.'KB';
        } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
            $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
        }
        return $file;
    } else { return $file; }
 
}
add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
function f711_image_size_prevent($file) {

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    if ( $type == $imagetype ) {

        // get filesize of upload
        $size = $file['size'];
        $size = $size / 1024; // Calculate down to KB

        // set sizelimit
        $limit = 700; // Your Filesize in KB

        // set imagelimit
        $imagelimit = 7;

        // query how many images the current user already uploaded
        global $current_user;
        $args = array(
            'orderby'         => 'post_date',
            'order'           => 'DESC',
            'numberposts'     => -1,
            'post_type'       => 'attachment',
            'author'          => $current_user->ID,
        );
        $attachmentsbyuser = get_posts( $args );

        if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
            $file['error'] = 'Image files must be smaller than '.$limit.'KB';
        } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
            $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
        }
        return $file;
    } else { return $file; }
}
added 1607 characters in body
Source Link
Yuri
  • 1.1k
  • 4
  • 25
  • 46

I use a function in my theme to reduce .jpg/.jpeg files size whenthat prevents upload of big size image files (thanks to @fischi), but I want to apply it strictly to .jpg/.jpeg files only, because it triesprevents to reduceupload .pdf files too. How to adapt this function?

add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
function f711_image_size_prevent($file) {

    // get filesize of upload
    $size = $file['size'];
    $size = $size / 1024; // Calculate down to KB

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set sizelimit
    $limit = 700; // Your Filesize in KB

    // set imagelimit
    $imagelimit = 7;

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    // query how many images the current user already uploaded
    global $current_user;
    $args = array(
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'numberposts'     => -1,
        'post_type'       => 'attachment',
        'author'          => $current_user->ID,
    );
    $attachmentsbyuser = get_posts( $args );

    if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
        $file['error'] = 'Image files must be smaller than '.$limit.'KB';
    } elseif ( $type != $imagetype ) { // check if image type is allowed
        $file['error'] = 'Image must be ' . $imagetype . '.';
    } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
        $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
    }
    return $file;

}

UPDATE

This is the improved code (not tested yet), thanks to @nikhil-sheth:

add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
function f711_image_size_prevent($file) {

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    if ( $type == $imagetype ) {

        // get filesize of upload
        $size = $file['size'];
        $size = $size / 1024; // Calculate down to KB

        // set sizelimit
        $limit = 700; // Your Filesize in KB

        // set imagelimit
        $imagelimit = 7;

        // query how many images the current user already uploaded
        global $current_user;
        $args = array(
                'orderby'         => 'post_date',
                'order'           => 'DESC',
                'numberposts'     => -1,
                'post_type'       => 'attachment',
                'author'          => $current_user->ID,
        );
        $attachmentsbyuser = get_posts( $args );

        if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
            $file['error'] = 'Image files must be smaller than '.$limit.'KB';
        } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
            $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
        }
        return $file;
    } else { return $file; }

}

I use a function in my theme to reduce .jpg/.jpeg files size when upload (thanks to @fischi), but I want to apply it strictly to .jpg/.jpeg files only, because it tries to reduce .pdf files too. How to adapt this function?

add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
function f711_image_size_prevent($file) {

    // get filesize of upload
    $size = $file['size'];
    $size = $size / 1024; // Calculate down to KB

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set sizelimit
    $limit = 700; // Your Filesize in KB

    // set imagelimit
    $imagelimit = 7;

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    // query how many images the current user already uploaded
    global $current_user;
    $args = array(
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'numberposts'     => -1,
        'post_type'       => 'attachment',
        'author'          => $current_user->ID,
    );
    $attachmentsbyuser = get_posts( $args );

    if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
        $file['error'] = 'Image files must be smaller than '.$limit.'KB';
    } elseif ( $type != $imagetype ) { // check if image type is allowed
        $file['error'] = 'Image must be ' . $imagetype . '.';
    } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
        $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
    }
    return $file;

}

I use a function in my theme that prevents upload of big size image files (thanks to @fischi), but I want to apply it strictly to .jpg/.jpeg files only, because it prevents to upload .pdf files too. How to adapt this function?

add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
function f711_image_size_prevent($file) {

    // get filesize of upload
    $size = $file['size'];
    $size = $size / 1024; // Calculate down to KB

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set sizelimit
    $limit = 700; // Your Filesize in KB

    // set imagelimit
    $imagelimit = 7;

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    // query how many images the current user already uploaded
    global $current_user;
    $args = array(
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'numberposts'     => -1,
        'post_type'       => 'attachment',
        'author'          => $current_user->ID,
    );
    $attachmentsbyuser = get_posts( $args );

    if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
        $file['error'] = 'Image files must be smaller than '.$limit.'KB';
    } elseif ( $type != $imagetype ) { // check if image type is allowed
        $file['error'] = 'Image must be ' . $imagetype . '.';
    } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
        $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
    }
    return $file;

}

UPDATE

This is the improved code (not tested yet), thanks to @nikhil-sheth:

add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
function f711_image_size_prevent($file) {

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    if ( $type == $imagetype ) {

        // get filesize of upload
        $size = $file['size'];
        $size = $size / 1024; // Calculate down to KB

        // set sizelimit
        $limit = 700; // Your Filesize in KB

        // set imagelimit
        $imagelimit = 7;

        // query how many images the current user already uploaded
        global $current_user;
        $args = array(
                'orderby'         => 'post_date',
                'order'           => 'DESC',
                'numberposts'     => -1,
                'post_type'       => 'attachment',
                'author'          => $current_user->ID,
        );
        $attachmentsbyuser = get_posts( $args );

        if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
            $file['error'] = 'Image files must be smaller than '.$limit.'KB';
        } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
            $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
        }
        return $file;
    } else { return $file; }

}
Source Link
Yuri
  • 1.1k
  • 4
  • 25
  • 46
Loading