Using WordPress ‘edit_custom_thumbnail_sizes’ PHP filter

The edit_custom_thumbnail_sizes WordPress PHP filter allows you to modify the available options for image editing by enabling custom sizes or specifying an array of custom size names.

Usage

add_filter('edit_custom_thumbnail_sizes', 'your_custom_function');

function your_custom_function($edit_custom_sizes) {
  // your custom code here
  return $edit_custom_sizes;
}

Parameters

  • $edit_custom_sizes (bool|string[]): True if custom sizes can be edited or an array of custom size names.

More information

See WordPress Developer Resources: edit_custom_thumbnail_sizes

Examples

Enable Custom Thumbnail Sizes for Image Editing

In this example, we enable all custom thumbnail sizes for image editing.

add_filter('edit_custom_thumbnail_sizes', 'enable_custom_thumbnail_sizes');

function enable_custom_thumbnail_sizes($edit_custom_sizes) {
  return true;
}

Disable Custom Thumbnail Sizes for Image Editing

In this example, we disable all custom thumbnail sizes for image editing.

add_filter('edit_custom_thumbnail_sizes', 'disable_custom_thumbnail_sizes');

function disable_custom_thumbnail_sizes($edit_custom_sizes) {
  return false;
}

Add Specific Custom Sizes for Image Editing

In this example, we specify an array of custom size names for image editing.

add_filter('edit_custom_thumbnail_sizes', 'add_specific_custom_sizes');

function add_specific_custom_sizes($edit_custom_sizes) {
  $custom_sizes = array('medium', 'large', 'extra-large');
  return $custom_sizes;
}

Remove a Specific Custom Size for Image Editing

In this example, we remove a specific custom size (‘medium’) from the available image editing options.

add_filter('edit_custom_thumbnail_sizes', 'remove_specific_custom_size');

function remove_specific_custom_size($edit_custom_sizes) {
  if (($key = array_search('medium', $edit_custom_sizes)) !== false) {
    unset($edit_custom_sizes[$key]);
  }
  return $edit_custom_sizes;
}

Add Custom Sizes Conditionally for Image Editing

In this example, we add custom sizes for image editing based on a user role.

add_filter('edit_custom_thumbnail_sizes', 'conditionally_add_custom_sizes');

function conditionally_add_custom_sizes($edit_custom_sizes) {
  if (current_user_can('editor')) {
    $custom_sizes = array('medium', 'large', 'extra-large');
    return $custom_sizes;
  }
  return $edit_custom_sizes;
}