WordPress:Function Reference/wp insert attachment
来自站长百科
Description
This function inserts an attachment into the media library. The function should be used in conjunction with wp_update_attachment_metadata() and wp_generate_attachment_metadata(). Returns the ID of the entry created in the wp_posts table.
Usage
%%% <?php wp_insert_attachment( $attachment, $filename, $parent_post_id ); ?> %%%
Example
To insert an attachment to a parent with a post ID of 37:
<?php $attach_id = wp_insert_attachment( $attachment, $filename, 37 ); $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); wp_update_attachment_metadata( $attach_id, $attach_data ); ?>
Parameters
Related
wp_get_attachment_url(), wp_delete_attachment(), wp_insert_post()