How to prevent woocommerce from creating new resized images? - php

Every time i upload a new image to the wordpress media library, wordpress generates 3 versions of the image with different sizes and Woocommerce also generates another 3 images.
I dont need 6 versions of the same image on my website. The 3 sizes generated by wordpress are enough.
My website contains alot of images, and i want to minimize the disk space on my server by preventing woocommerce from generating new unnecessary images.
I could not find any solution online.
Any ideas How to accomplish this?

You are right that woocommerce creates image sizes based on default WordPress sizes. But I won't recommend you to remove them because woocommerce expects these sizes and does not handle their absence ( does not revert to using default sizes ). So if you remove these sizes, you also have to write more code to adjust woocommerce for using default image sizes.
Now if you understand all this and still wish to do it. You can remove the woocommerce images sizes by using this snippet in functions.php or in a plugin.
function sr_remove_woocommerce_image_sizes() {
// Remove woocommerce copied sizes from default
remove_image_size( 'woocommerce_thumbnail' );
remove_image_size( 'woocommerce_single' );
remove_image_size( 'woocommerce_gallery_thumbnail' );
}
add_action( 'init', 'sr_remove_woocommerce_image_sizes', 99 );
Note: This won't delete all the existing image sizes, it will just stop creating them moving forward.

Related

Display a different image in different WordPress posts

I've tried to figure out how to display an image in different posts. When I create a post in WordPress it displays a featured image on each post. I have a custom DB with 1000 images and I want to display each image from my DB on each post. I would have 1000 posts, but each with its respective image from the DB, but I can't find where those php files are.
I've already done this in local host. I got a .php file which displays all these images, but I don't know if its just an issue of .php file or I have to code the functions in WordPress.
How about adding customization to your theme's functions.php file in WordPress that reroutes WordPress's default behavior for determining each featured image?
Note: Ideally these types of mods should be added to a child theme rather than the main theme so that updates to your theme do not overwrite your customization.
Here is an example of how this might work:
Added to /wp-content/themes/your-child-theme/functions.php
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html($html, $post_id, $post_image_id) {
// run a query to find the location of your preferred
// image in the alt location. $img is just an example.
$img = "/other_images/otherThumbnail-".$post_id.".jpg";
return '<img src="'.$img.'" alt="modified">';
}

Wordpress cannot change default size of featured image

On my blog I want to add an ability to attach featured images to posts. However those images by default are extremely small (150x150px) also wordpress automatically compress the size of them.
I tried to set a larger size two ways: in settings->media I changed size of thumbnails to 750x550px also in functions.php added this code:
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 750, 550, true );
But it change nothing and I still get 150x150px images. How can I fix it?
I use wordpress version 4.9.4
Wordpress doesn't alter thumbnails of existing images in your media after changing the settings.
You can use this plugin to regenerate thumbnails of your existing images :
Wordpress Regenerate Thumbnails Plugin

Wordpress thumbnail sizes

In my functions.php i defined 2 dimensions i want to use on my site, small and custom-size and my file looks like this:
add_theme_support('post-thumbnails');
add_image_size('small', 120, '', true);
add_image_size('custom-size', 755, 220, true);
When i check my wp-content/uploads folder, except original image and 120px and 755px images i see several others dimensions (150px, 768px) and i was wondering why is that showing if i only have 2 dimensions (i am asking because i am on limited hosting and want to save on disk space because i host a lot of images).
Thank you!
There are generally three reasons that these other sizes could be showing up:
You had different thumbnail dimensions / image sizes in the past. Images are not re-created (or deleted) when you change image sizes, but you can use a plugin such as Regenerate Thumbnails to do this for you - just click the options it adds under the Tools menu, after you have changed your image sizes.
You have a plugin or theme installed that is creating additional image sizes. To track down what is doing this, you can either search through the files for add_image_size, or you could deactive plugins/change themes and then regenerate your thumbnails to see what the result is.
Wordpress has a new hidden image size for responsive images. This applies in the case of the 768px image. See this WP Core blog post for details. If you have no need for responsive images you could change the size that applies to this image using the instructions in that blog post (running eg. update_option('medium_large_size_w', '755') or even manually updating the option in the database - it's in the wp_options table). Again - regenerate thumbnails after doing this.

Wordpress downloaded images resizes itself

I am using wordpress and woocommerce. I have one problem, i have created a system with downloads steam market items (names and pictures) and then creates products. But I have one problem, downloaded images that are used for product are automaticly resized to a different resolutions : 150x113 300x225 300x300 425x280 and original one. So it takes 5x space, which is a problem for me. How do i disable or fix that resizing problem.
Thanks.
I found this article here by WP Mayor. Basically you remove the code that produces them.
function wpmayor_filter_image_sizes( $sizes) {
unset( $sizes['medium']);
unset( $sizes['large']);
unset( $sizes['wysija-newsletters-max']);
return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'wpmayor_filter_image_sizes');
function wpmayor_custom_image_sizes($sizes) {
$myimgsizes = array(
"image-in-post" => __( "Image in Post" ),
"full" => __( "Original size" )
);
return $myimgsizes;
}
add_filter('image_size_names_choose', 'wpmayor_custom_image_sizes');
place this in your functions.php file
UPDATE:
Also look here - Seems you just go to the media page, and set the other sizes to 0
By default, WordPress generates three sizes of each image that you upload. Themes and plugins can generate additional images sizes aswell.
First thing you should keep in mind is that the default image sizes are not harmful for your website.
If you already know this but you want to proceed anyway (or your space is really limited), you will need to do some steps to make Wordpress just keep the original image:
First you need to understand how many copies of an uploaded image
are generated by WordPress. You can check that using FTP client and
accesing /wp-content/uploads/ directory.
You can stop WordPress from generating default image sizes by
visiting Settings » Media in WordPress admin area. There you will
see default image sizes predefined by WordPress. You need to set
these sizes to 0 which will prevent WordPress from generating
default image sizes when you upload a new image.
Finally your theme might be generating other thumbnails (check it in
step 1), if this is the case look into your theme’s
functions.php file. Simply look for the code line containing:
add_image_size( 'homepage-thumb', 220, 180, true );
and
set_post_thumbnail_size( 150, 150 );
Note: WordPress uses the thumbnail image size for galleries and theme pages so they might break after you prevent their generation.
If you need further information I suggest you to check the original source of this post.
Hope this helps you.
MAKING YOUR IMAGES LIGHTER (JPEG COMPRESSION HOOK).
It don't take "5x space" but between x1.5 and x3 max, because other sizes are lighter, depending of their dimension and number of different sizes.
But you can make your images even much more lighter with this native hook:
add_filter( 'jpeg_quality', create_function( '', 'return 80;' ) );
You can tune the compression value replacing 80 by a number from 0 to 100. This hook doesn't compress your original image, but only all the others. If you want to use it you will need to paste it in the function.php file of your active child theme or theme.
IMAGES SETTINGS IN WOOCOMMERCE:
They are located in left menu Woocommerce > Settings > Products > Display. You can change all values to 0 (similar to Wordpress Media settings).
As the others answers suggested, you can unset somme of them programatically. Is better to keep thumbnails, because they are used to display images in the media library.

Allowing For Custom Images Sizes with WordPress's Gallery Shortcode

In WordPress 2.5 and up, there's a built in Gallery feature that allows the option to add an image gallery to a Post or Page on your WordPress blog. (Ref: http://codex.wordpress.org/Gallery_Shortcode)
You can use a size option to specify the thumbnail size you would like displayed. Valid values include "thumbnail", "medium", "large" and "full". The default is "thumbnail". The size of the images for "thumbnail", "medium" and "large" can be configured in WordPress admin panel.
ie. [gallery size="medium"]
My Question: I'm trying to hack up the [gallery] shortcode to allow for custom sizes at the time of input -- not trying to do this through the admin panel. I'd like to use something like, [gallery size="145x160"].
Rather then download a bloated plugin, I'd rather work with what's already there and I'm not sure where I need to go in my file structure to make the changes. I'm familiar with PHP but I'm afraid I'll make a change and then when I update future versions of WP, it will overwrite what I've set in motion.
Could someone help me out with this?
Thank you very much!
I know this is late, but I found this question trying to accomplish the same thing.
The Gallery does not have any built-in filters to allow this, so I developed a solution that works below.
In your theme's functions.php file, add the following lines of code:
remove_shortcode('gallery');
add_shortcode('gallery', 'custom_size_gallery');
function custom_size_gallery($attr) {
// Change size here - medium, large, full
$attr['size'] = 'medium';
return gallery_shortcode($attr);
}
This will interrupt the normal gallery call, revise the size being used, and then call the built-in WordPress gallery.
Wordpress crunch the images in several size when you upload them. So you can not get your given size image unless you set it to the admin panel before uploading the image.
But you can add additional image size:
add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode
add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode
add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode
More about add_image_size() on Codex

Categories