Leave a Reply

2 comments

  1. 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);
    
  2. 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);