我想为在我的 WordPress 的不同页面上传的文件设置不同的规则,这可能吗?
到目前为止,我结束了这个,我认为这是正确的,但由于某种原因它仍然无法正常工作,如果您有时间调查并留下反馈,请。谢谢。
function up_filter( ) {
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$accURL = 'http://localhost/account/settings';
echo '<p>This is inserted at the bottom</p>';
if ($current_url == $accURL){
add_filter('wp_handle_upload_prefilter','mdu_validate_image_size');
function mdu_validate_image_size( $file ) {
// Calculate the image size in KB
$image_size = $file['size']/1024;
// File size limit in KB
$limit = 1000;
$image = getimagesize($file['tmp_name']);
$maximum = array(
'width' => '120',
'height' => '120'
);
$image_width = $image[0];
$image_height = $image[1];
$too_big = "File file size is too big. It needs to be smaller than $limit KB. Please save photos as jpg no larger than {$maximum['width']} by {$maximum['height']} pixels.";
$too_large = "File dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";
// Check if it's an image
$is_image = strpos($file['type'], 'image/gif');
if ( ( $image_size > $limit ) && ($is_image !== false) ) {
//add in the field 'error' of the $file array the message
$file['error'] = $too_big;
return $file;
}
elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
//add in the field 'error' of the $file array the message
$file['error'] = $too_large;
return $file;
}
else
return $file;
}
echo '<p>asdasd</p>';
}
}
add_action( 'wp', 'up_filter');