wp_check_filetype_and_ext

Home » Hooks » wp_check_filetype_and_ext

Hook Name: wp_check_filetype_and_ext

Explanation: The wp_check_filetype_and_ext hook is a WordPress filter hook that allows developers to modify or validate the file type and extension of an uploaded file. This hook is triggered when WordPress checks the file type and extension during the file upload process.

What it does: When a file is uploaded to WordPress, it goes through a series of checks to determine its file type and extension. The wp_check_filetype_and_ext hook is used to customize or validate these checks based on specific criteria set by the developer. By hooking into this filter, developers can modify the file type and extension validation process to suit their needs.

Why it’s useful: The wp_check_filetype_and_ext hook is particularly useful when you want to restrict or extend the types of files that can be uploaded to your WordPress site. For example, you may want to prevent certain file types from being uploaded or allow additional file types that are not supported by default. This hook empowers developers to enforce their own file type and extension restrictions, providing better control over the content that is uploaded to their WordPress site.

Example Usage: Let’s say you want to restrict the upload of certain file types, such as executable files (.exe) and script files (.js), to enhance the security of your WordPress site. You can achieve this by adding the following code to your theme’s functions.php file:

function restrict_filetypes($filetypes) {
    unset($filetypes['exe']);
    unset($filetypes['js']);
    return $filetypes;
}
add_filter('wp_check_filetype_and_ext', 'restrict_filetypes');

In this example, we define a function named restrict_filetypes that removes the ‘exe’ and ‘js’ file types from the $filetypes array. We then hook this function to the wp_check_filetype_and_ext filter using the add_filter() function. As a result, any attempt to upload files with these restricted extensions will be blocked by WordPress.

By utilizing the wp_check_filetype_and_ext hook, you can take control over the file type and extension validation process, allowing you to enhance the security and manageability of your WordPress website.

Learn More on WordPress.org

WordPress snippets using the wp_check_filetype_and_ext hook

Register an account to save your snippets or go Pro to get more features.