Checking file type in WordPress with wp_check_filetype

The wp_check_filetype function is a feature provided by WordPress that is responsible for validating the type of a given file. It checks the file extension and validates it against a predefined list of accepted file types. This function is predominantly used to ensure that the files being uploaded or manipulated within the WordPress environment are of the accepted types, thereby helping to maintain the integrity and security of the site.

By determining the type of the file, the wp_check_filetype function can help to manage the handling of different file types in a more controlled manner. For instance, it can help to prevent the uploading of potentially harmful or inappropriate file types, or it can be used to categorize files based on their types for better organization and retrieval.

The function returns an array with two elements: ‘ext’ which represents the extension of the file, and ‘type’ which represents the full MIME type. If the file type is not recognized, both elements in the array will be false.

Although the function is quite straightforward, it plays a crucial role in file management within WordPress, ensuring that only appropriate and accepted file types are used within the platform.

Parameters Accepted by the wp_check_filetype Function

The wp_check_filetype function in WordPress accepts two parameters. These are:

  • $filename (string). This parameter is mandatory and is used to specify the file name or path.
  • $mimes (string[]). This parameter is optional with a default value of null. It is an array of permitted mime types, each keyed by their respective file extension regex. If not specified, it defaults to the result of the get_allowed_mime_types() function.

Return Value of the wp_check_filetype Function

The wp_check_filetype function returns an array containing values for the extension and mime type. The details are as follows:

  • ext: This is a string that represents the file extension. If the file does not match a mime type, this value is false.
  • type: This is a string representing the file’s mime type. If the file doesn’t match a mime type, this value is also false.

If the function does not accept any parameters, it will be explicitly stated in a brief sentence.


How to Check the File Type of an Uploaded File

$file = $_FILES['uploaded_file'];
$filetype = wp_check_filetype( basename( $file['name'] ), null );

if ( $filetype['ext'] == 'jpg' || $filetype['ext'] == 'png' ) {
 echo "<p>The uploaded file is an image.</p>";
} else {
 echo "<p>The uploaded file is not an image.</p>";

In this example, the wp_check_filetype() function is used to check the file type of an uploaded file. The function returns an array that contains the extension and the mime type of the file. The code then checks if the extension is ‘jpg’ or ‘png’, and outputs a message accordingly.

How to Validate a File Type Before Uploading

$file = $_FILES['uploaded_file'];
$filetype = wp_check_filetype( basename( $file['name'] ), null );

if ( $filetype['ext'] == 'pdf' ) {
 // Upload the file
} else {
 echo "<p>Only PDF files are allowed.</p>";

This code snippet uses the wp_check_filetype() function to validate the file type before uploading. If the file is not a PDF, an error message is displayed.

How to Display the File Type of a Downloadable File

$file = 'path/to/downloadable/file';
$filetype = wp_check_filetype( $file, null );

echo "<p>The downloadable file is a " . $filetype['ext'] . " file.</p>";

This code snippet uses the wp_check_filetype() function to determine the file type of a downloadable file. The file type is then displayed to the user.


The wp_check_filetype function in WordPress is a utility that assists in determining the file type and extension of a given file. This function can be particularly useful in scenarios where the handling of different file types is required, such as uploading files, validating file types for security reasons, or categorizing files based on their types. By applying this function, developers can streamline the process of file management and enhance the functionality of their WordPress applications.

Related WordPress Functions