我编码前端张贴,这包括图像的元数据。我已经搜索了一些,想知道下面的代码,
function my_submission_processor() {
$post_data = array(
\'post_title\' => $_POST[\'post_title\'],
\'post_content\' => $_POST[\'post_content\'],
\'post_status\' => \'draft\'
);
$post_id = wp_insert_post( $post_data );
$upload = wp_upload_bits( $_FILES[\'image\'][\'name\'], null,
file_get_contents( $_FILES[\'image\'][\'tmp_name\'] ) );
$wp_filetype = wp_check_filetype( basename( $upload[\'file\'] ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
\'guid\' => $wp_upload_dir[\'baseurl\'] . _wp_relative_upload_path(
$upload[\'file\'] ),
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename( $upload[\'file\'] )),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$attach_id = wp_insert_attachment( $attachment, $upload[\'file\'], $post_id );
require_once(ABSPATH . \'wp-admin/includes/image.php\'); //this is what I wondering
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload[\'file\'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta( $post_id, \'_thumbnail_id\', $attach_id );
wp_redirect( site_url() . \'/thank-you/\' );
die();
}检查下面的代码行,
require_once(ABSPATH . \'wp-admin/includes/image.php\');
我不知道这是怎么回事,也不知道为什么会这样。这里需要php?以上代码来自此处。https://premium.wpmudev.org/blog/upload-file-functions/