admin管理员组文章数量:1410730
I am using a custom form to upload image and trying to move the file in the Media Library. The wp_insert_attachment() is working fine because I get the attachment id; however I don't see the image that I uploaded in the media library. Following is the code -
$image_url = $_FILES['input_14']['name'];
$upload_dir = wp_upload_dir();
$image_data = file_get_contents( $image_url );
$filename = basename( $image_url );
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
}
else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents( $file, $image_data );
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $file );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attachment_id , $file );
wp_update_attachment_metadata( $attachment_id , $attach_data );
It only shows the 'default' image file in media library when I submit the form.
Please advise what I am doing wrong. Thanks!
本文标签: uploadsMedia Library does not show the uploaded image
版权声明:本文标题:uploads - Media Library does not show the uploaded image 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744924157a2632486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论