有一个bug 在WordPress 4.7-4.7.3中,与验证MIME类型相关,因此Dave Romsey提供的代码无法工作。
有一个plugin in the repo 这将绕过MIME检查,但它禁用对所有用户和所有扩展的检查。我认为更好的方法是为管理员添加一个新功能,允许他们上传。csv扩展。
//* Register activation and deactivation hooks
register_activation_hook( __FILE__ , \'wpse_258192_activation\' );
register_deactivation_hook( __FILE__ , \'wpse_258192_deactivation\' );
//* Add upload_csv capability to administrator role
function wpse_258192_activation() {
$admin = get_role( \'administrator\' );
$admin->add_cap( \'upload_csv\' );
}
//* Remove upload_csv capability from administrator role
function wpse_258192_deactivation() {
$admin = get_role( \'administrator\' );
$admin->remove_cap( \'upload_csv\' );
}
//* Add filter to check filetype and extension
add_filter( \'wp_check_filetype_and_ext\', \'wpse_258192_check_filetype_and_ext\', 10, 4 );
//* If the current user can upload_csv and the file extension is csv, override arguments - edit - "$pathinfo" changed to "pathinfo"
function wpse_258192_check_filetype_and_ext( $args, $file, $filename, $mimes ) {
if( current_user_can( \'upload_csv\' ) && \'csv\' === pathinfo( $filename )[ \'extension\' ] ){
$args = array(
\'ext\' => \'csv\',
\'type\' => \'text/csv\',
\'proper_filename\' => $filename,
);
}
return $args;
}