wp_insert_attachment() – Inserts an attachment.

You appear to be a bot. Output may be restricted

Description

Inserts an attachment.

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.

Usage

$int|WP_Error = wp_insert_attachment( $args, $file, $parent_post_id, $wp_error, $fire_after_hooks );

Parameters

$args
( string|array ) required – Arguments for inserting an attachment.
$file
( string|false ) optional – Optional. Filename. Default false.
$parent_post_id
( int ) optional – Optional. Parent post ID or 0 for no parent. Default 0.
$wp_error
( bool ) optional – Optional. Whether to return a WP_Error on failure. Default false.
$fire_after_hooks
( bool ) optional default: 1 – Optional. Whether to fire the after insert hooks. Default true.

Returns

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

Source

File name: wordpress/wp-includes/post.php


Lines:

1 to 17 of 17
function wp_insert_attachment( $args, $file = false, $parent_post_id = 0, $wp_error = false, $fire_after_hooks = true ) {
  $defaults = array(
    'file'        => $file,
    'post_parent' => 0,
  );

  $data = wp_parse_args( $args, $defaults );

  if ( ! empty( $parent_post_id ) ) {
    $data['post_parent'] = $parent_post_id;
  }

  $data['post_type'] = 'attachment';

  return wp_insert_post( $data, $wp_error, $fire_after_hooks );
}
 

 View on GitHub View on Trac