Home / Attachments / Allow ICO Files Upload
Duplicate Snippet

Embed Snippet on Your Site

Allow ICO Files Upload

Enable ICO File Uploads on your WordPress website with this PHP code snippet.

Code Preview
php
<?php
function custom_allow_ico_upload($mimes) {
    $allowed_mime_types = array(
        'ico' => 'image/x-icon'
    );
    // Merge the allowed MIME types with the existing list
    $mimes = array_merge($mimes, $allowed_mime_types);
    return $mimes;
}
add_filter('upload_mimes', 'custom_allow_ico_upload');
function custom_handle_ico_upload_errors($file, $uploaded_file) {
    if (!empty($file['type']) && $file['type'] === 'image/x-icon' && !empty($uploaded_file['tmp_name'])) {
        // Check the file contents to ensure it's a valid ICO file
        if (mime_content_type($uploaded_file['tmp_name']) !== 'image/x-icon') {
            $file['error'] = __('Invalid ICO file.', 'text-domain');
        }
    }
    return $file;
}
add_filter('wp_handle_upload_prefilter', 'custom_handle_ico_upload_errors', 10, 2);

Comments

Add a Comment