Created
December 15, 2011 11:40
-
-
Save bueltge/1480813 to your computer and use it in GitHub Desktop.
Restrict list of allowed mime types and file extensions.
This file contains hidden or 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 | |
/** | |
* Plugin Name: Restrict mime types | |
* Plugin URI: http://wpengineer.com/?p=2369 | |
* Description: Restrict list of allowed mime types and file extensions. | |
* Version: 1.0.0 | |
* License: GPLv3 | |
* Author: Frank Bültge | |
* Author URI: http://bueltge.de/ | |
*/ | |
// This file is not called from WordPress. We don't like that. | |
! defined( 'ABSPATH' ) and exit; | |
// If the function exists this file is called as upload_mimes. | |
// We don't do anything then. | |
if ( ! function_exists( 'fb_restrict_mime_types' ) ) { | |
add_filter( 'upload_mimes', 'fb_restrict_mime_types' ); | |
/** | |
* Retrun allowed mime types | |
* | |
* @see function get_allowed_mime_types in wp-includes/functions.php | |
* @param array Array of mime types | |
* @return array Array of mime types keyed by the file extension regex corresponding to those types. | |
*/ | |
function fb_restrict_mime_types( $mime_types ) { | |
$mime_types = array( | |
'pdf' => 'application/pdf', | |
'doc|docx' => 'application/msword', | |
); | |
return $mime_types; | |
} | |
} | |
// If the function exists this file is called as post-upload-ui. | |
// We don't do anything then. | |
if ( ! function_exists( 'fb_restrict_mime_types_hint' ) ) { | |
// add to wp | |
add_action( 'post-upload-ui', 'fb_restrict_mime_types_hint' ); | |
/** | |
* Get an Hint about the allowed mime types | |
* | |
* @return void | |
*/ | |
function fb_restrict_mime_types_hint() { | |
echo '<br />'; | |
_e( 'Accepted MIME types: PDF, DOC/DOCX' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment