Created
October 31, 2025 16:41
-
-
Save joshuafredrickson/e4b855256b739a0e568b4dd8f4c3148e to your computer and use it in GitHub Desktop.
Add Alt Text and Dimensions columns to the Media Library
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
| /** | |
| * Add alt text and dimensions columns to the media library | |
| */ | |
| add_filter('manage_media_columns', function (array $columns): array { | |
| $columns['alt'] = 'Alt Text'; | |
| $columns['dimensions'] = 'Dimensions'; | |
| return $columns; | |
| }); | |
| /** | |
| * Display alt text and dimensions in the media library | |
| */ | |
| add_action('manage_media_custom_column', function (string $column_name, int $id): void { | |
| if ($column_name === 'dimensions') { | |
| $image = wp_get_attachment_metadata($id); | |
| echo $image['width'] . ' x ' . $image['height']; | |
| } elseif ($column_name === 'alt') { | |
| $alt = get_post_meta($id, '_wp_attachment_image_alt', true); | |
| echo $alt ?: '⚠️ No alt text'; | |
| } | |
| }, 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment