options - Modifying protexted image sizes: use "add_image_size()" or "update_option()"?

admin2025-01-08  3

I'm finding conflicting info on whether it's okay to override protected WordPress image sizes (thumbnail, medium, medium_large, and large) with add_image_size(). Some docs indicate it's better to do this with update_option (). Is there a practical difference between these sets of code:

function mytheme_image_sizes() {
  add_image_size('thumbnail', 200, 200, true);    
  add_image_size('medium', 480, 480, true); 
}

vs

function mytheme_image_sizes() {
  update_option( 'thumbnail_size_w', 200 );
  update_option( 'thumbnail_size_h', 200 );
  update_option( 'thumbnail_crop', 1 );
  update_option( 'medium_size_w', 480 );
  update_option( 'medium_size_h', 480 );
  update_option( 'medium_crop', 1 );
}

Note: I'm specifying these in my theme to stop admins from arbitrarily altering sizes and screwing up the post-4.4 responsive images function, and I'll probably remove the admin interface to change the image sizes.

I'm finding conflicting info on whether it's okay to override protected WordPress image sizes (thumbnail, medium, medium_large, and large) with add_image_size(). Some docs indicate it's better to do this with update_option (). Is there a practical difference between these sets of code:

function mytheme_image_sizes() {
  add_image_size('thumbnail', 200, 200, true);    
  add_image_size('medium', 480, 480, true); 
}

vs

function mytheme_image_sizes() {
  update_option( 'thumbnail_size_w', 200 );
  update_option( 'thumbnail_size_h', 200 );
  update_option( 'thumbnail_crop', 1 );
  update_option( 'medium_size_w', 480 );
  update_option( 'medium_size_h', 480 );
  update_option( 'medium_crop', 1 );
}

Note: I'm specifying these in my theme to stop admins from arbitrarily altering sizes and screwing up the post-4.4 responsive images function, and I'll probably remove the admin interface to change the image sizes.

Share Improve this question asked Apr 18, 2016 at 22:08 SlamSlam 3434 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

There is little difference, by default crop is set to false.

function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
    global $_wp_additional_image_sizes;

    $_wp_additional_image_sizes[ $name ] = array(
        'width'  => absint( $width ),
        'height' => absint( $height ),
        'crop'   => $crop,
    );
}

And also during the process of evaluation

if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
                $width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
                $height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
                $crop   = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop'];
            } else {
                $height = get_option( "{$size}_size_h" );
                $width  = get_option( "{$size}_size_w" );
                $crop   = ( $nocrop ) ? false : get_option( "{$size}_crop" );
            }

variable $_wp_additional_image_sizes is took in consideration. Other then that it is very simple, options will be read from options table only if not defined.

Also please note: the database wp_options table is read before functions.php has been processed.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265779a1069.html

最新回复(0)