i have a custom dropdown field on post and i would to get the value from that custom field and insert it on each image caption field uploaded to the current post.
I have been looking for a way to do this and found a couple of examples but not even one work. This morning i found a Wordpress filter that is called attachment_fields_to_save. To my surprise on the codex was an example of almost the same thing i was looking for but i didn't work.
This is the code
function insert_custom_default_caption($post, $attachment) {
if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
if ( strlen(trim($post['post_title'])) == 0 ) {
$post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
$post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
}
// captions are saved as the post_excerpt, so we check for it before overwriting
// if no captions were provided by the user, we fill it with our default
if ( strlen(trim($post['post_excerpt'])) == 0 ) {
$post['post_excerpt'] = 'default caption';
}
}
return $post . $attachment;
}
add_filter('attachment_fields_to_save', 'insert_custom_default_caption', 10, 2);
Can anyone help me to see what is wrong with that code?