Wordpress 阻止用户发布全尺寸图片上传 [英] Wordpress Prevent Users from posting full size image uploads

查看:23
本文介绍了Wordpress 阻止用户发布全尺寸图片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Wordpress 中是否有任何方法可以防止内容编辑者在将图片上传到帖子时选择全尺寸"选项?我希望它们只有缩略图"、中"和大"选项.我曾经使用 Scissors 插件来执行此操作,但从 Wordpress 2.9 开始,此插件不再有效.

解决方案

您可以通过强制 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) == '图像' ) {$alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);如果(空($alt))$alt = '';$form_fields['post_title']['required'] = true;$form_fields['image_alt'] = array('价值' =>$alt,'标签' =>__('替代文本'),'帮助' =>__('图片的替代文字,例如“蒙娜丽莎”'));$form_fields['align'] = array('标签' =>__('结盟'),'输入' =>'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') );} 别的 {取消设置($form_fields['image_alt']);}返回 $form_fields;}

此处与普通 WordPress 函数唯一不同的是,我们调用的是 MY_image_size_input_fields 而不是 image_size_input_fields.

现在进行实际隐藏的函数:

function MY_image_size_input_fields( $post, $check = '' ) {//获取此图像每个可能的中间版本的实际像素尺寸列表/* $size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('全尺寸'));*/$size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'));如果(空($检查))$check = get_user_setting('imgsize', 'medium');echo '

';print_r($check);echo '</pre>';foreach ( $size_names as $size => $label ) {$downsize = image_downsize($post->ID, $size);$checked = '';//这个尺寸可以选择吗?$enabled = ( $downsize[3] || 'large' == $size );$css_id = "图片尺寸-{$size}-{$post->ID}";//如果此大小是默认大小但不可用,请不要选择它如果 ( $size == $check ) {如果($启用)$checked = "checked='checked'";别的$check = '';} elseif ( !$check && $enabled && 'thumbnail' != $size ) {//如果未启用 $check,默认为第一个大于缩略图的可用尺寸$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>";//仅在该选项可用时才显示尺寸如果($启用)$html .= " 

';$out[] = $html;}返回数组('标签' =>__('尺寸'),'输入' =>'html','html' =>加入("\n", $out),);}

在最后一个函数中,只有两件事发生了变化.在顶部,我们去掉了 $size_names 数组定义中对全尺寸"的引用.然后说 $enabled = ( $downsize[3] || 'large' == $size ); 的那一行,我们改变了.我们只是将 'full' == $size 替换为 'large' == $size.

结果截图如下:

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. &#8220;The Mona Lisa&#8221;')
  );

  $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&nbsp;&times;&nbsp;%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:

这篇关于Wordpress 阻止用户发布全尺寸图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆