在wp_insert_attachment()

时间:2016-11-24 06:14:35

标签: wordpress

我正在编写一个插件,将电子邮件附件保存到自定义磁盘文件夹,然后将它们作为附件添加到自定义帖子类型中。到目前为止,这一切都正常,但我需要保存几个元数据值。元数据将始终相同(到目前为止),并且需要在任何附件文件类型(.zip,.jpg,.txt等)上

任何人都可以告诉我如何在此代码中添加元数据键/值:

$filename = $attachment['filename'];
$mimetype = $attachment['mimetype'];
$data     = $attachment['data'];

$upload = wp_upload_bits( $filename, null, $data );

if ( ! $upload['error'] ) {

    $attachment_data = array(
        'guid'           => trailingslashit( $upload['url']) . basename( $filename ),
        'post_mime_type' => $mimetype,
        'post_parent'    => $post_id,
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
        'post_content'   => '',
        'post_status'    => 'inherit',
    );


    $attachment_id = wp_insert_attachment( $attachment_data, $upload['file'], $post_id );

    if ( is_wp_error( $attachment_id ) ) {

        return false;

    } else {

        $attach_data = wp_generate_attachment_metadata( $attachment_id, $upload['file'] );
        wp_update_attachment_metadata( $attachment_id, $attach_data );

        return true;

    }
}
上面的 $ attach_data总是返回空。

0 个答案:

没有答案