我想为特定输入字段设置最小图像尺寸id= "job_logo"
在前端的提交表单中
根据这个答案How to Require a Minimum Image Dimension for Uploading?
以下代码设置了所有图像上载的限制
add_filter(\'wp_handle_upload_prefilter\',\'tc_handle_upload_prefilter\');
function tc_handle_upload_prefilter($file)
{
$img=getimagesize($file[\'tmp_name\']);
$minimum = array(\'width\' => \'640\', \'height\' => \'480\');
$width= $img[0];
$height =$img[1];
if ($width < $minimum[\'width\'] )
return array("error"=>"Image dimensions are too small. Minimum width is {$minimum[\'width\']}px. Uploaded image width is $width px");
elseif ($height < $minimum[\'height\'])
return array("error"=>"Image dimensions are too small. Minimum height is {$minimum[\'height\']}px. Uploaded image height is $height px");
else
return $file;
}
对于id=“job\\u logo”的特定字段,是否有办法做到这一点,即
以下是用于提交表单的wp job manager类别
https://github.com/Automattic/WP-Job-Manager/blob/c838d6ee3a3d0fd224d666bfee55f58517e10cf6/includes/forms/class-wp-job-manager-form-submit-job.php#L206
这是我用来添加上传图像字段的代码
add_filter( \'submit_job_form_fields\', \'my_custom_tax\' );
function my_custom_tax( $fields ) {
$fields[\'job\'][\'job_logo\'] = array(
\'label\' => __( \'Logo\', \'wp-job-manager\' ),
\'type\' => \'file\',
\'required\' => false,
\'placeholder\' => \'\',
\'priority\' => 1.15,
\'ajax\' => true,
\'multiple\' => false,
\'allowed_mime_types\' => array(
\'jpg\' => \'image/jpeg\',
\'jpeg\' => \'image/jpeg\',
\'png\' => \'image/png\'
)
);
return $fields;
}
以下是上载徽标图像字段的html输出
<input type="file" class="input-text wp-job-manager-file-upload" data-file_types="jpg|jpeg|png" name="job_logo" id="job_logo" placeholder="">
最合适的回答,由SO网友:Dave Romsey 整理而成
这个$file
数组没有任何您可以使用的内容,但是$GLOBALS
有一些有用的信息。您可以取消注释包含$GLOBALS
查看它包含的内容。
我在tc_handle_upload_prefilter
作用这样可以确保仅当job_logo
正在上载文件。
add_filter(\'wp_handle_upload_prefilter\',\'tc_handle_upload_prefilter\');
function tc_handle_upload_prefilter($file) {
// Debugging...
//return array("error"=> print_r( $_POST, true ) );
//return array("error"=> print_r( $file, true ) );
//return array("error"=> print_r( $GLOBALS, true ) ); // This is helpful...
//return array( "error"=> print_r( $GLOBALS[\'job_manager_uploading_file\'], true ) ); // Now we\'re cooking!
if ( ! isset( $GLOBALS[\'job_manager_uploading_file\'] ) || ( \'job_logo\' !== $GLOBALS[\'job_manager_uploading_file\'] ) ) {
return $file;
}
$img = getimagesize( $file[\'tmp_name\'] );
$minimum = array( \'width\' => \'640\', \'height\' => \'480\' );
$width = $img[0];
$height = $img[1];
if ( $width < $minimum[\'width\'] )
return array( "error"=>"Image dimensions are too small. Minimum width is {$minimum[\'width\']}px. Uploaded image width is $width px");
elseif ( $height < $minimum[\'height\'] )
return array("error"=>"Image dimensions are too small. Minimum height is {$minimum[\'height\']}px. Uploaded image height is $height px");
else
return $file;
}