# Photo Credit Field for Images

> Adds a "Photo credit" field to images in the Media Library and prints the credit under the image whenever it is used in an Image block.

- Type: PHP
- Runs: Run everywhere
- Category: [Media](https://wpide.com/library/category/media/)
- Tags: images, credits, attribution
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/photo-credit-field/

## Code

```php
add_filter( 'attachment_fields_to_edit', function ( $fields, $post ) {
    if ( wp_attachment_is_image( $post ) ) {
        $fields['wpide_photo_credit'] = array(
            'label' => 'Photo credit',
            'input' => 'text',
            'value' => get_post_meta( $post->ID, '_wpide_photo_credit', true ),
            'helps' => 'Shown below the image, for example "Jane Doe / Unsplash".',
        );
    }

    return $fields;
}, 10, 2 );

add_filter( 'attachment_fields_to_save', function ( $post, $attachment ) {
    if ( isset( $attachment['wpide_photo_credit'] ) && current_user_can( 'edit_post', $post['ID'] ) ) {
        $credit = sanitize_text_field( $attachment['wpide_photo_credit'] );

        if ( '' === $credit ) {
            delete_post_meta( $post['ID'], '_wpide_photo_credit' );
        } else {
            update_post_meta( $post['ID'], '_wpide_photo_credit', $credit );
        }
    }

    return $post;
}, 10, 2 );

add_filter( 'render_block_core/image', function ( $content, $block ) {
    $id     = isset( $block['attrs']['id'] ) ? (int) $block['attrs']['id'] : 0;
    $credit = $id ? get_post_meta( $id, '_wpide_photo_credit', true ) : '';

    if ( '' === $credit ) {
        return $content;
    }

    $html     = '<small class="wpide-photo-credit" style="display:block;font-size:.75em;opacity:.7;">Photo: ' . esc_html( $credit ) . '</small>';
    $position = strrpos( $content, '</figure>' );

    return false === $position ? $content . $html : substr_replace( $content, $html, $position, 0 );
}, 10, 2 );
```

## Notes

The credit is added below Image blocks that point to the attachment; classic editor images are not changed.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Photo Credit Field for Images" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
