0

The problem is: My featured image caption is not being shown since it is (still) stored in a custom field (called (wpcf-bildunterschrift). I can easily access the customfield, but not the field that WordPress stores its meta data for images in and since the meta data for the image isn't stored with the image I have to rely on the very post that uses the image to have a connection between both of them, the image and the data.

How can I copy the value from my post's custom field over to the caption-field of the featured image?

  • How are you displaying the post thumbnail and it's caption? You want to override that with the custom field value? – birgire Jun 27 '16 at 14:37
  • the post thumbnail is the featured image which is displayed in the posts layout. underneath should be the caption - which is yet empty since the value of that field is stored in a custom field of the very post. i could display the custom field underneath the image (with filters) when showing the post but I'd rather like to copy the (value of the) custom field into the meta of the used image (caption) ... – vittorio Jun 28 '16 at 9:02
1

Here's one way to inject the custom field as the featured image caption with help of the post_thumbnail_html filter:

add_filter( 'post_thumbnail_html', function( $html, $post_id )
{
    if( $html && $caption = get_post_meta( $post_id, 'wpcf-bildunterschrift', true ) )
        $html .= sprintf( '<p>%s</p>', sanitize_text_field( $caption ) );

    return $html;
}, 10, 2 );

Hopefully you can adjust this to your needs.

Update

According to ticket #12235 we will have a new template tag, in WP 4.6+, to display featured image caption, namely:

the_post_thumbnail_caption() 

where it's output is filterable through the the_post_thumbnail_caption filter.

It's a wrapper for the function:

get_the_post_thumbnail_caption() 

That's again wrapper for the function:

wp_get_attachment_caption()

This function fetches the caption from the attachment's post_excerpt field and it's output is filterable through the wp_get_attachment_caption filter.

Example

If our theme used this new template tag, then we could filter it with:

add_filter( 'the_post_thumbnail_caption', function( $caption )
{
    if( $custom = get_post_meta( get_the_ID(), 'wpcf-bildunterschrift', true ) )
        $caption = sanitize_text_field( $custom );

    return $caption;
}, 10, 2 );

Note that here we e.g. strip out the possible HTML tags from the custom field. If you want to support HTML then you can check out the discussion in the ticket.

  • thank you very much! while this is working on a display-level I was hoping to find a solution that reads the post, takes the value from the custom field and saves it as the caption of the used image though. but i really appreciate your effort and will probably use your approach if I don't find another solution that does what I was looking for... cheers! – vittorio Jun 28 '16 at 11:01
  • You're welcome. The image caption is stored in the wp_posts table under post_excerpt column. Here are some questions that come to mind: like why you need to copy the value from the custom field to the post_excerpt. When should it copy? What if the user changes the caption the normal way, wouldn't it go in async with the custom field? What would happen if you switched featured images? Wouldn't you override the previous image caption? If the caption (post_excerpt) and custom field are to be synced, why does it matter which one is fetched? @vittorio – birgire Jun 28 '16 at 13:17
  • ... and some further questions comes to mind. So I think there are too many unknowns for me here to go further ;-) @vittorio – birgire Jun 28 '16 at 13:17

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.