Wordpress阻止用户发布完整大小的图片上传

时间:2010-01-05 16:08:05

标签: wordpress

Wordpress中是否有任何方法可以阻止内容编辑者在将图像上传到帖子时选择“全尺寸”选项?我希望他们只有“缩略图”,“中等”和“大”选项。我以前使用Scissors插件来做到这一点,但是从Wordpress 2.9开始,这个插件不再有效。

2 个答案:

答案 0 :(得分:4)

您可以通过强制WordPress不显示完整大小选项来实现此结果。创建大小单选按钮的功能位于wp-admin/includes/media.php,称为image_size_input_fields

我知道该函数没有过滤器或动作挂钩,但调用它的函数(image_attachment_fields_to_edit)有一个attachment_fields_to_edit的过滤器挂钩。

所以基本上我们可以使用过滤器钩子来覆盖我们自己的两个函数,只会稍加修改。

这可以在标准的functions.php文件中使用,或者我想你可以将它合并到一个插件中。

首先,添加新过滤器:

add_filter('attachment_fields_to_edit', 'MY_image_attachment_fields_to_edit', 11, 2);

接下来我们创建两个函数。在这种情况下,我只使用MY_作为名称前缀:

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

我们在正常的WordPress功能中改变的唯一一点就是我们正在调用MY_image_size_input_fields而不是image_size_input_fields

现在执行实际隐藏的功能:

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&nbsp;&times;&nbsp;%d)"), $downsize[1], $downsize[2] ). "</label>";

   $html .= '</div>';

   $out[] = $html;

  }

  return array(
   'label' => __('Size'),
   'input' => 'html',
   'html'  => join("\n", $out),
  );
}

在最后一个函数中,只有两件事发生了变化。在顶部,我们摆脱了$ size_names数组定义中对'Full Size'的引用。然后是$enabled = ( $downsize[3] || 'large' == $size );的行,我们改变了。我们只是将'full' == $size替换为'large' == $size

以下是结果的屏幕截图: alt text

答案 1 :(得分:0)

我认为您不能禁用大尺寸,但您可以将其宽度和高度设置为与中等尺寸相同。它位于设置→媒体