Skip to content

Instantly share code, notes, and snippets.

@gwin
Created May 26, 2016 09:08
Show Gist options
  • Save gwin/109d8611c9dec35cfdbe8756bbd78cbf to your computer and use it in GitHub Desktop.
Save gwin/109d8611c9dec35cfdbe8756bbd78cbf to your computer and use it in GitHub Desktop.
<?php
// Assign a filter
add_filter( 'wphd_file_upload', 'my_wphd_file_upload' );
/**
* Check if uploaded file extension is valid.
*
* This function is executed using 'wphd_file_upload' filter,
* it stops file upload using die() if incorrect file type is sent
*
* @param array $file Element of $_FILES array
* @return array
*/
function my_wphd_file_upload( $file ) {
$allowed = array("pdf", "zip", "jpeg", "jpg", "gif", "png", "doc", "docx");
$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
if( ! in_array( $ext, $allowed ) ) {
$response = new stdClass();
$response->result = 0;
$response->msg = sprintf("Uploading file wit extension '%s' is not allowed.", $ext);
die(json_encode($response));
}
return $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment