WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Difference between revisions of "Function Reference/wp insert attachment"

(Added line number to Trac location)
m (updating 'guid' code)
Line 20: Line 20:
 
$wp_upload_dir = wp_upload_dir();
 
$wp_upload_dir = wp_upload_dir();
 
$attachment = array(
 
$attachment = array(
'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $filename ),
+
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
 
'post_mime_type' => $wp_filetype['type'],
 
'post_mime_type' => $wp_filetype['type'],
 
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
 
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),

Revision as of 02:52, 11 December 2012

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 ); ?> 

Parameters

$attachment
(array) (required) Array of data about the attachment that will be written into the wp_posts table of the database. Must contain at a minimum the keys post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type.
Default: None
$filename
(string) (optional) Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be on the uploads directory. See wp_upload_dir()
Default: false
$parent_post_id
(int) (optional) Attachments are associated with parent posts. This is the ID of the parent's post ID.
Default: 0

Return Values

Returns the resulting post ID (int) on success - likely returns 0 on failure.

Example

To insert an attachment to a parent with a post ID of 37:

<?php
  $wp_filetype = wp_check_filetype(basename($filename), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
     'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 
     'post_mime_type' => $wp_filetype['type'],
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
     'post_content' => '',
     'post_status' => 'inherit'
  );
  $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
  // you must first include the image.php file
  // for the function wp_generate_attachment_metadata() to work
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  wp_update_attachment_metadata( $attach_id, $attach_data );
?>

Note

Using the _wp_relative_upload_path() to build the guid may not be reliable on some servers.

Source File

wp_insert_attachment() is located in wp-includes/post.php.

Related

wp_get_attachment_url(), wp_delete_attachment(), wp_insert_post()

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.