返回介绍

wp_insert_attachment()

发布于 2017-09-11 12:13:30 字数 5533 浏览 994 评论 0 收藏 0

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

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


参数

$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


返回值

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


源代码

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

更新日志

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

相关函数

Uses

  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/post.php: wp_insert_post()

Used By

  • wp-admin/includes/class-wp-site-icon.php: WP_Site_Icon::insert_attachment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_crop_image()
  • wp-admin/includes/class-file-upload-upgrader.php: File_Upload_Upgrader::__construct()
  • wp-admin/includes/image.php: wp_generate_attachment_metadata()
  • wp-admin/includes/media.php: media_handle_upload()
  • wp-admin/includes/media.php: media_handle_sideload()
  • wp-admin/includes/import.php: wp_import_handle_upload()
  • wp-admin/custom-header.php: Custom_Image_Header::insert_attachment()
  • wp-admin/custom-header.php: Custom_Image_Header::step_2_manage_upload()
  • wp-admin/custom-background.php: Custom_Background::handle_upload()
  • wp-includes/post.php: wp_update_post()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_newMediaObject()
  • Show 7 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note 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 );
    ?>
  2. Returns int 0 on failure

    Usage

    <?php wp_insert_attachment( $attachment, $filename, $parent_post_id ); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文