Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Okay, I've been working on this all day and can't find the solution anywhere. I've created a custom field within the image upload screen (info first found here). The field stores the name of a photographer to give him/her credit. Everything's fine there.

What I can't figure out is how to get this information into every image's title data across the site. I want my field for Image Credit to append itself to all image titles across the site, so each image title would read something like "My Title (by Photographer Name)".

What I have figured out is close, but not quite good enough. I can get the information from my custom field by using "get_children()" of type "image" on the existing post, and then calling "get_post_meta()" for my custom field name on each result. I can then use this information to add it to the post featured images by altering the title properties within "get_the_post_thumbnail()" when I call it. What I need is to be able to do something like this site-wide on all images, not just the featured ones.

I feel like there should be somewhere in the Core that defines how images are displayed. Perhaps I could overwrite this with a same-name file in my own theme, or by declaring a function to override the default behavior. Maybe there's a filter or a hook I could use. However, I can't find any documentation to help me, and I'm about out of ideas.

Any and all thoughts would be much appreciated, even if they're just partial ideas.

Note: I tried to link all my functions for easy reference, but spam-prevention wouldn't let me post that many links so I'll list them here instead:

  • codex.wordpress.org/Function_Reference/get_children
  • codex.wordpress.org/Function_Reference/get_post_meta
  • codex.wordpress.org/Function_Reference/get_the_post_thumbnail
share|improve this question
    
Hi @Roxanne - It would be helpful if you could provide the code you are using that displays your image. There is more than one way to display an image so without us being able to see how you display it we might have to do a lot of irrelevant work to try to help you. –  MikeSchinkel Sep 16 '11 at 3:29

2 Answers 2

up vote 1 down vote accepted

You can create a filter using the "wp_get_attachment_image_attributes" hook. Place this in your functions.php file.

function filter_image_title($attr, $attachment = null){
        //Find your $photographer with $attachment->ID
        $attr['title'] .= ' (' . __('Photographed by', 'foobar') . ' ' . $photographer . ')';
        return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'filter_image_title', 10, 2);
share|improve this answer
    
That looks like exactly what I'm after, thank you! I'll have to look into it in more detail tomorrow, but that looks like the exact hook I couldn't track down. I'm excited to give it a shot in the morning. –  Roxanne Ready Sep 16 '11 at 6:40

Thanks to @Brian, I've written the following. However, for some reason this is still only affecting featured images. If the post has no featured image, it does not affect the first image as I might have expected. I completely commented out my previous code which was designed to affect the featured image, so it can't be a holdover. Any ideas?

function filter_image_title($attr, $attachment = null){
    //Find photo credit with $attachment->ID
    $attachment_credit = get_post_meta($attachment->ID, '_waz-image-credit', true);

    //Store original image info
    $attr['title'] = get_post($attachment->ID)->post_title;
    $attr['alt'] = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);

    //If there's no ALT text, use the title (pre-credit addition)
    if(!$attr['alt'])
        $attr['alt'] = $attr['title'];

    //If a credit has been added to the image, add this to the title
    if($attachment_credit) 
        $attr['title'] .= ' (' . 'Photographed by' . ' ' . $attachment_credit . ')';

    return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'filter_image_title', 10, 2);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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