Cole
Cole

Reputation: 1503

How can I add custom image sizes to wordpress but have them in the admin?

I know how to use add_image_size in PHP to register a new size which can be used in templates. It makes images in this size, too when they're uploaded.

The sizes I added like that don't show in the admin, though.

So 2 questions...

Or am I doing something wrong, like not putting the add_image_size call in the right place/file? Are sizes like this supposed to show up in the upload menu section?

Cole

Upvotes: 2

Views: 1778

Answers (1)

Richard M
Richard M

Reputation: 14545

Wordpress doesn't show custom image size options in the media lightbox, but you can add them using an attachment_fields_to_edit filter. The below will add options for all the custom image sizes you have defined.

add_filter('attachment_fields_to_edit', 'my_attachment_fields_to_edit_filter', 100, 2);

function my_attachment_fields_to_edit_filter($form_fields, $post) {
  if (!array_key_exists('image-size', $form_fields)) return $form_fields;

  global $_wp_additional_image_sizes;
  foreach($_wp_additional_image_sizes as $size => $properties) {
    if ($size == 'post-thumbnail') continue;

    $label = ucwords(str_replace('-', ' ', $size));
    $cssID = "image-size-{$size}-{$post->ID}";

    $downsize = image_downsize($post->ID, $size);
    $enabled = $downsize[3];

    $html = '<input type="radio" ' . disabled($enabled, false, false) . 'name="attachments[' . $post->ID. '][image-size]" id="' . $cssID . '" value="' . $size .'">';
    $html .= '<label for="'. $cssID . '">' . $label . '</label>';
    if ($enabled) $html .= ' <label for="' . $cssID . '" class="help">(' . $downsize[1] . '&nbsp;×&nbsp;' . $downsize[2] . ')</label>';
    $form_fields['image-size']['html'] .= '<div class="image-size-item">' . $html . '</div>';
  }

  return $form_fields;
}

Upvotes: 8

Related Questions