3

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?

  • 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
    @brasofilo +1 for some very welcoming portuguese on WPSE. :) – moraleida May 30 '12 at 22:05
3

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 :)

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.