Search

איך להגביל את גודל התמונות שניתן להעלות לספריית המדיה בוורדפרס

ישנן מצבים בהם תרצו להגביל את עורכי התוכן באתר מלהעלות תמונות מעבר לגודל מסויים. ניתן אף להיות ספציפים יותר ולאפשר העלאת תמונות בגודל ספציפי ומדוייק ברמת הפיקסל. הוסיפו את הקוד הבא לקובץ functions.php:

/* 
 * Check image resolution (px) before crunching
 */
function savvy_validate_image_size( $file ) {
    $image = getimagesize($file['tmp_name']);
    $minimum = array(
        'width' => '460',
        'height' => '460'
    );
    $maximum = array(
        'width' => '1280',
        'height' => '1280'
    );
    $image_width = $image[0];
    $image_height = $image[1];

    $too_small = "Image dimensions are too small. Minimum size is {$minimum['width']} by {$minimum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";
    $too_large = "Image dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";

    if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
        // add in the field 'error' of the $file array the message 
        $file['error'] = $too_small;
        return $file;
    }
    elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
        //add in the field 'error' of the $file array the message
        $file['error'] = $too_large;
        return $file;
    }
    else
        return $file;
}
add_filter('wp_handle_upload_prefilter','savvy_validate_image_size');

באפשרותכם לשנות את גובה ורוחב המינימום והמקסימום של התמונה (פיקסלים) בשורות 6-12. אתם מוזמנים אגב לתת מבט על פוסט רחב יותר המדבר על גודל התמונות בוורדפרס.

מצאתם טעות בקוד? הסניפט לא עובד לכם? רישמו לי בתגובות ואני מבטיח לטפל בכך במהרה ולספק סניפט תקין...
0 תגובות...

תגובה חדשה

Up!
לבלוג