Created
May 26, 2016 09:08
-
-
Save gwin/109d8611c9dec35cfdbe8756bbd78cbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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