Using WordPress ‘attachment_icon’ PHP filter

The attachment_icon WordPress PHP filter allows you to customize the icon displayed for attachments in the media library and posts.

Usage

add_filter('attachment_icon', 'your_custom_function', 10, 3);
function your_custom_function($icon, $post_id, $size) {
    // your custom code here
    return $icon;
}

Parameters

  • $icon (string): The URL of the default icon for the attachment.
  • $post_id (int): The ID of the attachment.
  • $size (string|array): The size of the icon. Can be a named size (e.g. ‘thumbnail’) or an array of width and height values.

More information

See WordPress Developer Resources: attachment_icon

Examples

Change the default icon for PDF attachments

This code replaces the default icon for PDF attachments with a custom icon.

add_filter('attachment_icon', 'custom_pdf_icon', 10, 3);
function custom_pdf_icon($icon, $post_id, $size) {
    $mime_type = get_post_mime_type($post_id);

    if ($mime_type == 'application/pdf') {
        $icon = 'https://yourdomain.com/wp-content/uploads/custom-pdf-icon.png';
    }

    return $icon;
}

Change the default icon for all audio files

This code replaces the default icon for all audio files with a custom icon.

add_filter('attachment_icon', 'custom_audio_icon', 10, 3);
function custom_audio_icon($icon, $post_id, $size) {
    $mime_type = get_post_mime_type($post_id);

    if (wp_attachment_is('audio', $post_id)) {
        $icon = 'https://yourdomain.com/wp-content/uploads/custom-audio-icon.png';
    }

    return $icon;
}

Use FontAwesome icons for specific file types

This code uses FontAwesome icons for specific file types instead of the default icons.

add_filter('attachment_icon', 'fontawesome_attachment_icons', 10, 3);
function fontawesome_attachment_icons($icon, $post_id, $size) {
    $mime_type = get_post_mime_type($post_id);

    switch ($mime_type) {
        case 'application/pdf':
            $icon = '<i class="fas fa-file-pdf"></i>';
            break;
        case 'application/zip':
            $icon = '<i class="fas fa-file-archive"></i>';
            break;
    }

    return $icon;
}

Change the default icon based on attachment size

This code replaces the default icon for large attachments with a custom icon.

add_filter('attachment_icon', 'custom_large_attachment_icon', 10, 3);
function custom_large_attachment_icon($icon, $post_id, $size) {
    $file_size = filesize(get_attached_file($post_id));

    if ($file_size > 1048576) { // 1 MB
        $icon = 'https://yourdomain.com/wp-content/uploads/custom-large-icon.png';
    }

    return $icon;
}

Change the default icon for specific user roles

This code replaces the default icon for attachments uploaded by users with the ‘editor’ role with a custom icon.

add_filter('attachment_icon', 'custom_editor_attachment_icon', 10, 3);
function custom_editor_attachment_icon($icon, $post_id, $size) {
    $author_id = get_post_field('post_author', $post_id);
    $user = get_userdata($author_id);
    if (in_array('editor', $user->roles)) {
        $icon = 'https://yourdomain.com/wp-content/uploads/custom-editor-icon.png';
    }

return $icon;
}