if ($_FILES) {
$uploaddir = wp_upload_dir();
$file = $_FILES[featured_image];
$uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file[\'name\'] );
move_uploaded_file( $file[\'tmp_name\'] , $uploadfile );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'menu_order\' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.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( $new_job_id, $attach_id );
}
图像上传正确,并设置为所选帖子的特色图像。图像的显示也与普通图像一样(除了只能显示完整的图像大小,不能加载缩略图大小。)但在后端的媒体库中,图像显示为带有折叠角的页面,您无法看到实际图像。
我做错了什么?
最合适的回答,由SO网友:Tom J Nowell 整理而成
看来你已经走上了冗长的路线,手动完成了上传的每一步。
但是,为什么不使用为您完成这一切的侧加载功能呢?您的大部分代码可以替换为:
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
require_once( ABSPATH . \'wp-admin/includes/media.php\' );
$attach_id = media_handle_upload( \'featured_image\', $new_job_id );
if ( is_wp_error( $attach_id ) ) {
// There was an error uploading the image.
} else {
// The image was uploaded successfully!
}
从那里你可以设置你的帖子缩略图等,查阅文档
media_handle_upload
如需了解更多详细信息,请记住向上载程序添加临时检查,否则您的网站可能会遭到黑客攻击
SO网友:lukgoh
if ($_FILES) {
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
$uploaddir = wp_upload_dir();
$file = $_FILES[featured_image];
$file_return = wp_handle_upload( $file, array(\'action\' => \'tm_add_new_job\' ) );
if( isset( $file_return[\'error\'] ) || isset( $file_return[\'upload_error_handler\'] ) ) {
return false;
} else {
$filename = $file_return[\'file\'];
$attachment = array(
\'post_mime_type\' => $file_return[\'type\'],
\'post_title\' => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $filename ) ),
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'guid\' => $file_return[\'url\']
);
$attachment_id = wp_insert_attachment( $attachment, $file_return[\'url\'] );
// Generate the metadata for the attachment, and update the database record.
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $new_job_id, $attachment_id );
}
}