插入附件。
原型
wp_insert_attachment( string|array $args, string $file = false, int $parent, bool $wp_error = false )
描述
如果在$ args参数中设置“ID”,则表示你正在更新并尝试更新附件。你还可以通过设置键’post_name’或’post_title’来设置附件名称或标题。
参考:
- wp_insert_post()
参数
$args
(string|array)
(Required)
插入附件的参数。
$file
(string)
(Optional)
文件名。
$parent
(int)
(Optional)
家长帖子ID。
$wp_error
(bool)
(Optional)
是否在失败时返回WP_Error。
返回值
(int|WP_Error)
附件ID成功。值0或
源文件
路径:wp-includes/post.php
<?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 );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_insert_attachment/