0

I'm trying to find a way to automatically add a photographer credit line to user-inserted photos. I've added custom fields to the media uploader for 'Photographer Name' and 'Photographer URL' but I can't figure out a way to automatically insert the input from these fields into the caption field so that they will automatically display with the photo.

Here's the code to generate the custom fields:

function attachment_field_credit( $form_fields, $post ) {
$form_fields['photographer-name'] = array(
    'label' => 'Photographer Name',
    'input' => 'text',
    'value' => get_post_meta( $post->ID, 'photographer_name', true ),
    'helps' => 'If provided, photo credit will be displayed',
);

$form_fields['photographer-url'] = array(
    'label' => 'Photographer URL',
    'input' => 'text',
    'value' => get_post_meta( $post->ID, 'photographer_url', true ),
    'helps' => 'Add Photographer URL',
);  

return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );

Thanks for your help.

0

I tried to do this once and almost got almost. Here's what I did learn. Depending on your needs, this solution may be workable.

You can append content to media when it's inserted into the editor using the get_image_tag filter:

/**
 * Filter to get caption and append image information to it.
 *
 * @return text HTML content describing with media attribution
 **/

add_filter('get_image_tag', 'my_append_media_credit', 20, 2);

function my_append_media_credit( $html, $id ) {

    $my_media_string = /* get your fields and put them together nicely */;

    $html = $html . $my_media_string;

    return $html;

}

However, you'll notice that I mentioned it only works for images when they're inserted into posts (and possibly updated, though I'd need to check out that). So if you're starting a site from scratch, that may get you far enough, but I got stuck on retroactively applying it to existing images in posts.

You could get some additional coverage for images with captions by filtering img_caption_shortcode which filters the [caption] shortcode output but that still leaves images without captions and already in posts uncovered.


UPDATE 10/23/13 2:20pm PST:

Here's the function I used to generate the $my_media_string in the code above:

/**
 * Function to lookup media creator fields and put them together.
 *
 * returns an html string with creator name and license name links.
 **/

function my_media_credit_string( $my_media_id ) {

    $my_name = get_post_meta( $my_media_id, 'my_creator_name', true );
    $my_name_url = get_post_meta( $my_media_id, 'my_creator_url', true );

    $my_creator = null;
    if( $my_name ) {
        if( $my_name_url ) {
            $my_creator = ' <a href="' . $my_name_url . '">' . $my_name . '</a>.';  
        } else {
            $my_creator = ' ' . $my_name;
        }
    }

    return '<span class="my-media-attr">Credit:' . $my_creator . '</span>';

}

So the first snippet would get this:

    $my_media_string = $my_media_credit_string( $id );
  • Thanks, this didn't quite get me where I need to be, but it definitely helped. Is there a way to assign a function as the value of $my_media_string? My method didn't seem to work. – Sean Henderson Oct 23 '13 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.