Is there any way in Wordpress to prevent content editors from selecting the "Full size" option when uploading images to a post? I'd like them to just have the "thumbnail", "medium", and "large" options. I used to use the Scissors plugin to do this, but as of Wordpress 2.9 this plugin no longer works.
You could acheive this result by forcing WordPress not to display the full size option. The function that creates the size radio buttons is in wp-admin/includes/media.php
and is called image_size_input_fields
.
There's no filter or action hook for that function that I'm aware of, but the function that calls it (image_attachment_fields_to_edit
) has a filter hook of attachment_fields_to_edit
.
So basically we can use the filter hook to override those two functions with our own, which will only be very slightly modified.
This will work in the standard functions.php file, or I suppose you could incorporate it into a plugin.
First, add the new filter:
add_filter('attachment_fields_to_edit', 'MY_image_attachment_fields_to_edit', 11, 2);
Next we create our two functions. I've just prefixed the names with MY_
for this case:
function MY_image_attachment_fields_to_edit($form_fields, $post) {
if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
$alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
if ( empty($alt) )
$alt = '';
$form_fields['post_title']['required'] = true;
$form_fields['image_alt'] = array(
'value' => $alt,
'label' => __('Alternate text'),
'helps' => __('Alt text for the image, e.g. “The Mona Lisa”')
);
$form_fields['align'] = array(
'label' => __('Alignment'),
'input' => 'html',
'html' => image_align_input_fields($post, get_option('image_default_align')),
);
$form_fields['image-size'] = MY_image_size_input_fields( $post, get_option('image_default_size', 'medium') );
} else {
unset( $form_fields['image_alt'] );
}
return $form_fields;
}
The only thing that's changed here from the normal WordPress function is that we're calling MY_image_size_input_fields
instead of image_size_input_fields
.
Now the function that does the actual hiding:
function MY_image_size_input_fields( $post, $check = '' ) {
// get a list of the actual pixel dimensions of each possible intermediate version of this image
/* $size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full size')); */
$size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'));
if ( empty($check) )
$check = get_user_setting('imgsize', 'medium');
echo '<pre>'; print_r($check); echo '</pre>';
foreach ( $size_names as $size => $label ) {
$downsize = image_downsize($post->ID, $size);
$checked = '';
// is this size selectable?
$enabled = ( $downsize[3] || 'large' == $size );
$css_id = "image-size-{$size}-{$post->ID}";
// if this size is the default but that's not available, don't select it
if ( $size == $check ) {
if ( $enabled )
$checked = " checked='checked'";
else
$check = '';
} elseif ( !$check && $enabled && 'thumbnail' != $size ) {
// if $check is not enabled, default to the first available size that's bigger than a thumbnail
$check = $size;
$checked = " checked='checked'";
}
$html = "<div class='image-size-item'><input type='radio' " . ( $enabled ? '' : "disabled='disabled' " ) . "name='attachments[$post->ID][image-size]' id='{$css_id}' value='{$size}'$checked />";
$html .= "<label for='{$css_id}'>$label</label>";
// only show the dimensions if that choice is available
if ( $enabled )
$html .= " <label for='{$css_id}' class='help'>" . sprintf( __("(%d × %d)"), $downsize[1], $downsize[2] ). "</label>";
$html .= '</div>';
$out[] = $html;
}
return array(
'label' => __('Size'),
'input' => 'html',
'html' => join("\n", $out),
);
}
In this last function only two things change. At the top we get rid of the reference to 'Full Size' in the $size_names array definition. Then the line that says $enabled = ( $downsize[3] || 'large' == $size );
, we changed. We simply replaced 'full' == $size
with 'large' == $size
.
Here's a screenshot of the result: