How to provide image credit in WordPress

Depending on the source of the images you are using, you might need to provide credit when using them. Or you just might want to do it, to appreciate the work of the author. Either way, you’ve come to the right place.

Based on the suggestion of Eric Weston, and inspired by the article at Tips and Tricks HQ, the StockPack plugin was adapted to allow citations for all images, not only the images downloaded via our plugin.

Enabling the image credit fields in WordPress

Enabling support for image credit fields is straightforward. First, you need to install the StockPack plugin from the WordPress repository and activate it.

Then you need to go to the settings page, to the Advanced tab, and enable the caption settings as shown below:

Enable caption settings

That’s it, now you will see new fields when editing an image from the media library. The featured image caption is optional, and it

Supported image credit fields

Supported image credit fields

You will be able to provide image credit to all the images in the Media Library including featured images. The credit supports the following fields:

  • Author Name
  • Author URL
  • Source Name
  • Source URL
  • License Name
  • License URL
  • Modifications

Autocomplete support for standard licenses

Some licenses are automatically completed for you. If you have one that you use often, please reach out so we can add it to the list. Here’s the current list of licenses that we autocomplete:

To use the autocomplete, just type one of the license abbreviations in the “Source” field and then move to another.

If you are more technical, you can add new options using the stockpack_autocomplete_licenses filter like so:

<?php


function stockpack_add_new_autocomplete_license( $options, $attachment ) {
    // custom-new-slug is what you will type
    $options['custom-new-slug'] = array(
        'name' => 'Custom New License', // this is the name it will expand to
        'url'  => 'https://custom.url' // this is the url it will use
    );
    // you can duplicate the lines above to add more options like so
//    $options['custom-new-slug2'] = array(
//        'name' => 'Second Custom New License', // this is the name it will expand to
//        'url'  => 'https://second-custom.url' // this is the url it will use
//    );

    return $options;
}

add_filter( 'stockpack_autocomplete_licenses', 'stockpack_add_new_autocomplete_license', 10, 2 );

Changing the generated caption

While StockPack already has a filter for captions that are automatically pulled from the API here, the captions that are generated work differently.

To change the generated caption you can use the filter stockpack_generated_caption

Here’s how you can construct your own caption. It’s a long function because there are a lot of parts, but it’s very simple. Also, you can change what gets generated based on the source if you need different attribution based on the image source.

<?php

function stockpack_change_generated_caption( $caption, $attachment ) {
    $caption = __( 'Photo from', 'stockpack' ) . ' ';
    /** Author */
    if ( isset( $attachment['stockpack_author_url'] ) && $attachment['stockpack_author_url'] && ( isset( $attachment['stockpack_author_name'] ) ) && $attachment['stockpack_author_name'] ) {
        $caption .= '<a href="' . $attachment['stockpack_author_url'] . '">';
    }
    if ( isset( $attachment['stockpack_author_name'] ) && $attachment['stockpack_author_name'] ) {
        $caption .= $attachment['stockpack_author_name'];
    }

    if ( isset( $attachment['stockpack_author_url'] ) && $attachment['stockpack_author_url'] && ( isset( $attachment['stockpack_author_name'] ) ) && $attachment['stockpack_author_name'] ) {
        $caption .= '</a>';
    }
    /** End Author */


    /** Provider/Image title/Image Source */
    if ( ( isset( $attachment['stockpack_provider'] ) && $attachment['stockpack_provider'] ) ) {
        $caption .= ' ' . __( 'on', 'stockpack' ) . ' ';
    }

    if ( isset( $attachment['stockpack_image_url'] ) && $attachment['stockpack_image_url'] && ( isset( $attachment['stockpack_provider'] ) ) && $attachment['stockpack_provider'] ) {
        $caption .= '<a href="' . $attachment['stockpack_image_url'] . '">';
    }

    if ( isset( $attachment['stockpack_provider'] ) && $attachment['stockpack_provider'] ) {
        $caption .= $attachment['stockpack_provider'];
    }

    if ( isset( $attachment['stockpack_image_url'] ) && $attachment['stockpack_image_url'] && ( isset( $attachment['stockpack_provider'] ) ) && $attachment['stockpack_provider'] ) {
        $caption .= '</a>';
    }
    /** End Provider/Image title/Image Source */


   /** License */
    if ( ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) || ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] ) ) {
        $caption .= ' ' . __( 'used under', 'stockpack' ) . ' ';
    }

    if ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] && ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) ) {
        $caption .= '<a href="' . $attachment['stockpack_license_url'] . '">';
    }

    if ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) {
        $caption .= $attachment['stockpack_license'];
    }

    if ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] && ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) ) {
        $caption .= '</a>';
    }
    /** End License */

    if ( isset( $attachment['stockpack_modification'] ) && $attachment['stockpack_modification'] ) {
        $caption .= ' / ' . $attachment['stockpack_modification'];
    }

    return $caption;
}

add_filter( 'stockpack_generated_caption', 'stockpack_change_generated_caption', 10, 2 );

Featured image caption

As mentioned above, you can automatically show the caption for WordPress images using the setting under Advanced. This relies on a filter that the theme you are using should be using.

You can also call the function manually in your loop stockpack_featured_image_caption()

If you have any questions, please reach out or write to us in the comments.

Leave a Comment