WordPress插入附件信息到数据库函数:wp_insert_attachment

WordPress函数wp_insert_attachment用于插入一个附件信息到数据库。

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

函数参数

$args

数组

该参数可用的值可以参考wp_insert_post()函数,如果指定了ID,则更新现有附件信息。

$file

字符串

文件名

$parent

整数

父ID,例如要将该附件归属在哪篇文章下。

$wp_error

布尔值,默认值:false

发生错误时,是否返回WP_Error

$fire_after_hooks

布尔值,默认值:true

是否启用插入附件后的hooks

函数使用示例

wp_insert_attachment(
    array(
        'guid' => $upload['url'],
        'post_title' => sanitize_text_field( $title ),
        'post_excerpt' => sanitize_text_field( $caption ),
        'post_content' => sanitize_text_field( $description ),
        'post_mime_type' => $response_headers['content-type'],
    ),
    $upload['file'],
    0
);

扩展阅读

wp_insert_attachment()函数位于:wp-includes/post.php

相关函数:

阿里云