On this page
wp_insert_attachment( string|array $args, string|false $file = false, int $parent, bool $wp_error = false, bool $fire_after_hooks = true ): int|WP_Error
Inserts an attachment.
Description
If you set the ‘ID’ in the $args parameter, it will mean that you are updating and attempt to update the attachment. You can also set the attachment name or title by setting the key ‘post_name’ or ‘post_title’.
You can set the dates for the attachment manually by setting the ‘post_date’ and ‘post_date_gmt’ keys’ values.
By default, the comments will use the default settings for whether the comments are allowed. You can close them manually or keep them open by setting the value for the ‘comment_status’ key.
See also
Parameters
$argsstring|array Required-
Arguments for inserting an attachment.
$filestring|false Optional-
Filename.
Default:
false $parentint Optional-
Parent post ID.
$wp_errorbool Optional-
Whether to return a WP_Error on failure.
Default:
false $fire_after_hooksbool Optional-
Whether to fire the after insert hooks.
Default:
true
Return
int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
Source
File: wp-includes/post.php. View all references
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) {
$defaults = array(
'file' => $file,
'post_parent' => 0,
);
$data = wp_parse_args( $args, $defaults );
if ( ! empty( $parent ) ) {
$data['post_parent'] = $parent;
}
$data['post_type'] = 'attachment';
return wp_insert_post( $data, $wp_error, $fire_after_hooks );
}
Related
Uses
| Uses | Description |
|---|---|
| wp_insert_post() wp-includes/post.php | Inserts or update a post. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Attachments_Controller::edit_media_item() wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Applies edits to a media item and creates a new attachment record. |
| WP_REST_Attachments_Controller::insert_attachment() wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Inserts the attachment post in the database. Does not update the attachment meta. |
| WP_Site_Icon::insert_attachment() wp-admin/includes/class-wp-site-icon.php | Inserts an attachment. |
| wp_ajax_crop_image() wp-admin/includes/ajax-actions.php | Ajax handler for cropping an image. |
| File_Upload_Upgrader::__construct() wp-admin/includes/class-file-upload-upgrader.php | Construct the upgrader for a form. |
| wp_generate_attachment_metadata() wp-admin/includes/image.php | Generates attachment meta data and create image sub-sizes for images. |
| media_handle_upload() wp-admin/includes/media.php | Saves a file submitted from a POST request and create an attachment post for it. |
| media_handle_sideload() wp-admin/includes/media.php | Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload() . |
| wp_import_handle_upload() wp-admin/includes/import.php | Handles importer uploading and adds attachment. |
| Custom_Image_Header::insert_attachment() wp-admin/includes/class-custom-image-header.php | Insert an attachment and its metadata. |
| Custom_Image_Header::step_2_manage_upload() wp-admin/includes/class-custom-image-header.php | Upload the file to be cropped in the second step. |
| Custom_Background::handle_upload() wp-admin/includes/class-custom-background.php | Handles an Image upload for the background image. |
| wp_update_post() wp-includes/post.php | Updates a post with new post data. |
| wp_xmlrpc_server::mw_newMediaObject() wp-includes/class-wp-xmlrpc-server.php | Uploads a file, following your settings. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_insert_attachment