WordPress yüklenen resim boyutunu sınırlamak

Yazı veya sayfaya resim yüklerken çok küçük ya da çok büyük olmasını engellemek için genişlik ve yükseklik değerlerine sınır getirebiliriz. function.php dosyasına ekleyeceğimiz bir fonksiyon bunu sağlayabilir.

add_filter('wp_handle_upload_prefilter','mdu_validate_image_size');
function mdu_validate_image_size( $file ) {
    $image = getimagesize($file['tmp_name']);
    $minimum = array(
        'width' => '320',
        'height' => '500'
    );
    $maximum = array(
        'width' => '1500',
        'height' => '1000'
    );
    $image_width = $image[0];
    $image_height = $image[1];

    $too_small = "Yüklenen görüntünün boyutu çok küçük. Genişlik minimum {$minimum['width']}px ve yükseklik minimum {$minimum['height']}px olmalı. Yüklenen dosya $image_width x $image_height piksel.";
    $too_large = "Yüklenen görüntünün boyutu çok büyük. Genişlik maksimum {$maximum['width']}px ve yükseklik maksimum {$maximum['height']}px olmalı. Yüklenen dosya $image_width x $image_height piksel.";

    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;
}

Yazıya resim eklerken width height değerlerini eklememek

WordPresste herhangi bir yazıya resim eklediğimizde width height değerleri ile birlikte eklenir. Eğer bunu istemiyorsak, function.php dosyasına aşağıdaki satırları eklemeliyiz;

add_filter( 'get_image_tag', 'remove_width_and_height_attribute', 10 );
add_filter( 'use_default_gallery_style', '__return_false' );
add_filter( 'post_thumbnail_html', 'remove_width_and_height_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_and_height_attribute', 10 );
function remove_width_and_height_attribute( $html ) {
   return preg_replace( '/(height|width)="\d*"\s/', "", $html );
}