wp_insert_attachment( string|array $args, string $file = false, int $parent, bool $wp_error = false )

Insert an attachment.


Description 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.


Parameters Parameters

$args

(string|array) (Required) Arguments for inserting an attachment.

$file

(string) (Optional) Filename.

Default value: false

$parent

(int) (Optional) Parent post ID.

$wp_error

(bool) (Optional) Whether to return a WP_Error on failure.

Default value: false


Top ↑

Return Return

(int|WP_Error) The attachment ID on success. The value 0 or WP_Error on failure.


Top ↑

Source Source

File: wp-includes/post.php

function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false ) {
	$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 );
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Added the $wp_error parameter to allow a WP_Error to be returned on failure.
2.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Example
    Inserts an attachment to a parent with a post ID of 37:

    <?php 
    // $filename should be the path to a file in the upload directory.
    $filename = '/path/to/uploads/2013/03/filename.jpg';
    
    // The ID of the post this attachment is for.
    $parent_post_id = 37;
    
    // Check the type of file. We'll use this as the 'post_mime_type'.
    $filetype = wp_check_filetype( basename( $filename ), null );
    
    // Get the path to the upload directory.
    $wp_upload_dir = wp_upload_dir();
    
    // Prepare an array of post data for the attachment.
    $attachment = array(
    	'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
    	'post_mime_type' => $filetype['type'],
    	'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
    	'post_content'   => '',
    	'post_status'    => 'inherit'
    );
    
    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
    
    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    
    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );
    
    set_post_thumbnail( $parent_post_id, $attach_id );
    ?>

You must log in before being able to contribute a note or feedback.