0

Is there a way to add a default description for media files as soon as they are uploaded to WordPress?

Specifically, I want to modify the code here to extract the text from PDF files and include it in the description for the media item in WP.

I've found that adding a filter using the "attachment_fields_to_save" hook will allow me to change the description (post_content) for media, but it only works after I click "update" on the Edit Media screen. When items are first uploaded, the description is empty by default.

Thanks for any help!

1 Answer 1

7

If we look at the source of wp_insert_attachment() we find the hooks edit_attachment and add_attachment. In your case we could use the add_attachment hook. The callback takes the attachment's ID as a parameter. By looking at the database table wp_posts we see that the description is saved as post_content and the caption as post_excerpt:

Looking inside the wp_posts database table

Then you could try this snippet:

/**
 * Default title, caption and description to uploaded attachments
 *
 * @param integer $post_ID The attachment's ID
 * @return void
 */

function wpse_121709_upload_defaults( $post_ID )
{
    $args = array( 
                    'ID'           => $post_ID, 
                    'post_title'   => 'My default title ...', 
                    'post_excerpt' => 'My default caption ...', 
                    'post_content' => 'My default description ...', 
            );

    wp_update_post( $args );

}
add_action( 'add_attachment', 'wpse_121709_upload_defaults' );

It should give you this result when you upload your file in the media uploader:

Default title, caption and description

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.