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.

As the title of this answer say, I want to define a default caption to all uploaded images in my WordPress... Is there a way to do something like this?

share|improve this question
1  
Bem-vinda ao WPSE, Miriam! - You may want to check some notification apps (stackapps.com) that will help you to follow up your Questions and Answers here. Saudações :) –  brasofilo May 30 '12 at 21:08
1  
Hello @brasofilo! Thank you! =) –  Miriam de Paula May 30 '12 at 21:57
1  
@brasofilo +1 for some very welcoming portuguese on WPSE. :) –  moraleida May 30 '12 at 22:05

1 Answer 1

up vote 2 down vote accepted

There's really no documentation for it yet, but you'll probably be able to do it hooking to the attachment_fields_to_save filter and inserting the default caption there.

From the Codex:

attachment_fields_to_save
applied to fields associated with an attachment prior to saving them in the database. Called in the media_upload_form_handler function. Filter function arguments: an array of post attributes, an array of attachment fields including the changes submitted from the form

It is defined on wp-admin/includes/media.php:

// TESTED :)
function wpse300512_image_attachment_fields_to_save($post, $attachment) {
if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
    if ( '' === trim( $post['post_title'] ) ) {
        $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 ( '' === trim( $post['post_excerpt'] ) ) {
        $post['post_excerpt'] = 'default caption';
    }
}

return $post;
}

add_filter('attachment_fields_to_save', 'wpse300512_image_attachment_fields_to_save', 10, 2);

UPDATE: I managed to test it, and it works as is. Just drop it on your functions.php :)

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.