简体   繁体   中英

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...

  • How can I get the image sizes that I make show up in the 'upload image' thing when bloggers make a new post?

  • Is there a way to list it in the admin so the bloggers can add new ones without having to change PHP code?

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM