Table of contents
Get the industry’s fastest WordPress hosting◦ 100% network uptime
◦ Comprehensive security
◦ 24/7 support

WordPress GuideImages → Custom Fields

Add a custom field box to WordPress media files

A man in a collared shirt and glasses smiles at the camera as he finishes setting up his WordPress hosting

The WordPress media library keeps your images and files organized, but what if you need to store extra information—like a photo credit, license type, or custom label? That’s where custom fields come in. 

If you’ve ever wished for a way to add your own fields to media items, you’re not alone, and yes, it’s definitely possible.

Get fast, reliable hosting for WordPress

Power your site with the industry’s fastest, most optimized WordPress hosting

Two ways to add a custom field box to media files

You can add custom field boxes either by using a plugin or by writing custom code. A plugin is the best choice if you want a quick solution with an interface to manage fields, while coding is ideal if you prefer a lightweight, flexible method that doesn’t rely on third-party tools.

1. Add a custom field box with a WordPress plugin

Plugins make this process beginner-friendly by giving you an interface to create and display fields. Here are two good options:

Let’s walk through adding a custom field to media files using Meta Box:

2. Add custom field boxes to media files without a plugin

If you’re comfortable editing your theme’s functions.php file or building a custom plugin, you can add custom field boxes with code. This method is flexible and doesn’t depend on extra plugins.

Here’s how to do it:

Here’s a simple example that adds a “Photo Credit” field to media files:

// Add custom field to media edit screen
function add_photo_credit_field($form_fields, $post) {
    $form_fields[‘photo_credit’] = array(
        ‘label’ => ‘Photo Credit’,
        ‘input’ => ‘text’,
        ‘value’ => get_post_meta($post->ID, ‘photo_credit’, true),
        ‘helps’ => ‘Enter the name of the photographer.’
    );
    return $form_fields;
}
add_filter(‘attachment_fields_to_edit’, ‘add_photo_credit_field’, 10, 2);
// Save the custom field
function save_photo_credit_field($post, $attachment) {
    if (isset($attachment[‘photo_credit’])) {
        update_post_meta($post[‘ID’], ‘photo_credit’,
sanitize_text_field($attachment[‘photo_credit’]));
    }
    return $post;
}
add_filter(‘attachment_fields_to_save’, ‘save_photo_credit_field’, 10, 2);

After adding this code, a new “Photo Credit” field will appear when you edit any media file. The value will be stored in the database as post meta and can be retrieved anywhere in your theme.

How to use custom fields for media

Custom fields in WordPress enhance media library management by allowing you to add valuable metadata to your files. This is useful in various scenarios:

By adding these custom fields, you can easily filter and search for specific media based on these attributes, making your library more organized and efficient.

Best practices for adding a custom field to WordPress media

Before you dive in, let’s look at some best practices. It’s easy for custom fields to get out of hand and messy.

Ready to get started?

Get the fastest, most secure WordPress.org hosting on the market.

Trust us to help you choose the ideal hosting solution

Loading form…