How to save attachment data?

WordPress has an hook for save meta data an attachment; an small example; like the post_data on save posts.

// Construct the attachment array
    $attachment = array_merge( array(
        'post_mime_type' => $type,
        'guid' => $url,
        'post_parent' => $post_id,
        'post_title' => $title,
        'post_content' => $content,
    ), $post_data );

    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);

See more on the code of media.php, function media_handle_upload
For Update the data use the hook wp_update_attachment_metadata(), see source

Also you can add fields and data on attachment, maybe this post helps you.

Leave a Comment