函数文档

remove_image_size()

💡 云策文档标注

概述

remove_image_size() 函数用于移除已注册的自定义图片尺寸,适用于主题或插件中需要替换或禁用特定图片尺寸的场景。该函数不能用于保留的默认图片尺寸名称。

关键要点

  • 参数 $name 为字符串类型,必需,指定要移除的图片尺寸名称。
  • 返回值:布尔值,成功移除返回 true,失败返回 false。
  • 主要用途:当插件注册了图片尺寸,而主题想使用相同名称但不同尺寸时,可先移除再添加。
  • 限制:不能用于保留的图片尺寸名称(如 'thumbnail', 'medium', 'large' 等)。
  • 注意事项:仅影响未来上传的图片,现有图片需通过其他工具(如“重新生成缩略图”插件)处理。

代码示例

// 移除自定义图片尺寸示例
function wpdocs_remove_plugin_image_sizes() {
    remove_image_size( 'image-name' );
}
add_action('init', 'wpdocs_remove_plugin_image_sizes');

// 移除并重新添加图片尺寸以修改属性
function wpdocs_remove_then_add_image_sizes() {
    remove_image_size( 'image-name' );
    add_image_size( 'image-name', 200, 200, true );
}
add_action('init', 'wpdocs_remove_then_add_image_sizes');

// 移除所有非默认图片尺寸
function remove_extra_image_sizes() {
    foreach ( get_intermediate_image_sizes() as $size ) {
        if ( !in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
            remove_image_size( $size );
        }
    }
}
add_action('init', 'remove_extra_image_sizes');

注意事项

  • 对于保留的默认图片尺寸(如 'medium'),不能直接使用 remove_image_size(),但可通过设置尺寸为 0px 或使用 intermediate_image_sizes_advanced 等变通方法处理。
  • 移除图片尺寸有助于节省磁盘空间,但仅对新上传图片生效,现有图片需额外清理。

📄 原文内容

Removes a new image size.

Parameters

$namestringrequired
The image size to remove.

Return

bool True if the image size was successfully removed, false on failure.

More Information

  • Useful when a plugin has registered an image size and you want to use the same image name in your theme but with a different size.
  • Cannot be used on reserved image size names.

Source

function remove_image_size( $name ) {
	global $_wp_additional_image_sizes;

	if ( isset( $_wp_additional_image_sizes[ $name ] ) ) {
		unset( $_wp_additional_image_sizes[ $name ] );
		return true;
	}

	return false;
}

Changelog

Version Description
3.9.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    To remove all the image sizes keeping only the default WordPress image sizes –

    function remove_extra_image_sizes() {
        foreach ( get_intermediate_image_sizes() as $size ) {
            if ( !in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
                remove_image_size( $size );
            }
        }
    }
    
    add_action('init', 'remove_extra_image_sizes');

  2. Skip to note 6 content

    Another common case is when your (parent) theme, or a plugin creates image sizes that you will never use.
    Deregistering unwanted sizes will prevent WordPress from generating and storing images you won’t use.
    This will save disk space, and associated costs.

    Of course, it only affects future image uploads.
    Plugins that “regenerate thumbnails” can be used to delete existing thumbnails that are not currently registered, while creating any missing sizes.

    The article mentions that you cannot use this function on reserved image size names.
    However there are a few work arounds:

    • In the WordPress Settings, you can set the image size to 0px.
    • You can try the `intermediate_image_sizes_advanced` function.
    • You can put the following code in your functions.php file:
      update_option( 'thumbnail_size_h', 0 );
      update_option( 'thumbnail_size_w', 0 );
      update_option( 'medium_size_h', 0 );
      update_option( 'medium_size_w', 0 );
      update_option( 'large_size_h', 0 );
      update_option( 'large_size_w', 0 );

  3. Skip to note 7 content

    Example

    In a theme’s functions.php file:

    function wpdocs_remove_plugin_image_sizes() {
    	remove_image_size( 'image-name' );
    }
    add_action('init', 'wpdocs_remove_plugin_image_sizes');

    You could combine this with the add_image_size function in your theme.

    function wpdocs_remove_then_add_image_sizes() {
    	remove_image_size( 'image-name' );
    	add_image_size( 'image-name', 200, 200, true );
    }
    add_action('init', 'wpdocs_remove_then_add_image_sizes');

  4. Skip to note 8 content

    To replace any of the default image sizes like ‘medium’, ‘large’, etc you must remove first and then add with new attributes.

    /**
     * Add crop attribute to 'medium' WordPress default image size
     */
    add_action( 'init', 'wpdocs_change_medium_image_size' );
    function wpdocs_change_medium_image_size() {
        remove_image_size( 'medium' );
        add_image_size( 'medium', 300, 300, true );
    }