Merhaba, functions.php içerisine ekleyeceğiniz bu kod ile web sitenize yükleyeceğiniz bütün fotoğrafları otomatik olarak optimize etmesini sağlayabilirsiniz.

$editor->set_quality( 70 ); // Resim kalitesi (0-100) satırından resmin kalite düzeyini belirleyebilirsiniz. 10 - En düşük kalite maksimum sıkıştırma 100 - En yüksek kalite minimum sıkıştırma. Kabul edilir değer 70'tir.

// Ürün resimlerini optimize etme
add_filter( 'jpeg_quality', function($arg){return 80;} );
add_filter( 'wp_editor_set_quality', function($arg){return 80;} );

function compress_product_images( $attachment_id ) {
    $attachment = get_post( $attachment_id );
    $mime_type = $attachment->post_mime_type;

    if ( strpos( $mime_type, 'image' ) !== false ) {
        $upload_dir = wp_upload_dir();
        $image_path = get_attached_file( $attachment_id );
        $image_url = wp_get_attachment_url( $attachment_id );
        $image_name = basename( $image_url );

        // Resmi optimize etme
        if ( function_exists( 'wp_get_image_editor' ) ) {
            $editor = wp_get_image_editor( $image_path );

            if ( ! is_wp_error( $editor ) ) {
                $editor->set_quality( 70 ); // Resim kalitesi (0-100)
                $editor->save( $image_path );
            }
        }
    }
}

add_action( 'add_attachment', 'compress_product_images' );
add_action( 'edit_attachment', 'compress_product_images' );
add_action( 'pre_post_update', 'compress_product_images' );