Skip to content

Instantly share code, notes, and snippets.

@jameskoster
Last active September 19, 2024 12:40
Show Gist options
  • Select an option

  • Save jameskoster/1695731 to your computer and use it in GitHub Desktop.

Select an option

Save jameskoster/1695731 to your computer and use it in GitHub Desktop.
WooCommerce - set image dimensions upon theme activation
<?php
/**
* Hook in on activation
*/
/**
* Define image sizes
*/
function yourtheme_woocommerce_image_dimensions() {
global $pagenow;
if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
return;
}
$catalog = array(
'width' => '400', // px
'height' => '400', // px
'crop' => 1 // true
);
$single = array(
'width' => '600', // px
'height' => '600', // px
'crop' => 1 // true
);
$thumbnail = array(
'width' => '120', // px
'height' => '120', // px
'crop' => 0 // false
);
// Image sizes
update_option( 'shop_catalog_image_size', $catalog ); // Product category thumbs
update_option( 'shop_single_image_size', $single ); // Single product image
update_option( 'shop_thumbnail_image_size', $thumbnail ); // Image gallery thumbs
}
add_action( 'after_switch_theme', 'yourtheme_woocommerce_image_dimensions', 1 );
@sumonbdinfo

Copy link
Copy Markdown

Should I put it on WooCommerce-function.php file or on template function.php file?

@inesrodrigues

Copy link
Copy Markdown

Hello. It comes from here: http://docs.woothemes.com/document/set-woocommerce-image-dimensions-upon-theme-activation/

So, i believe it might be added to the theme functions.php ;)

@Ansif

Ansif commented Apr 16, 2014

Copy link
Copy Markdown

It's not working in my theme :(

i put this into my theme but not effect in next install with new database?

@StevenJohnRobson

StevenJohnRobson commented Jun 17, 2016 •

Copy link
Copy Markdown

This works only on activation of the theme, so you can either trick it by changing theme and going back, or just using:

    $catalog = array(
        'width'     => '400',   // px
        'height'    => '400',   // px
        'crop'      => 1        // true
    );
    $single = array(
        'width'     => '600',   // px
        'height'    => '600',   // px
        'crop'      => 1        // true
    );
    $thumbnail = array(
        'width'     => '120',   // px
        'height'    => '120',   // px
        'crop'      => 0        // false
    );
    // Image sizes
    update_option( 'shop_catalog_image_size', $catalog );       // Product category thumbs
    update_option( 'shop_single_image_size', $single );         // Single product image
    update_option( 'shop_thumbnail_image_size', $thumbnail );   // Image gallery thumbs

At the bottom of functions.php and it worked fine, although this works for my theme because I don't want people to be able to change the sizes.

@kieusonlam

Copy link
Copy Markdown

Hello guys. Is there anyway i can change thumbnail crop position by using this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment