I am working on a simple front end image upload in Wordpress. With this piece of code I can upload to the wordpress media section but I only upload the name and url of the file then, no image.
Does anyone know what I need to do to upload the image also?
My form:
<form id="dash_action" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input name="submit" type="submit" class="button" value="<?php _e( 'Save settings' ); ?>" />
</form><!--End dash_action-->
My PHP form handling:
if( isset( $_POST['submit'] ) ) {
$filename = $_FILES['file']['name'];
$wp_filetype = wp_check_filetype( basename($filename), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['subdir'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, 37 );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
After the upload is finished, image file is stored in temporary folder, and will be deleted if you do not copy it somewhere. You can get the file with
$_FILES['file']['tmp_name'];
That will give you file path, which you can move with
move_uploaded_file();
where you need it.
The WordPress codex states that "The file MUST be on the uploads directory". I've added a line to move the uploaded file into the uploads directory and changed the guid path.
if( isset( $_POST['submit'] ) ) {
$filename = $_FILES['file']['name'];
$wp_filetype = wp_check_filetype( basename($filename), null );
$wp_upload_dir = wp_upload_dir();
// Move the uploaded file into the WordPress uploads directory
move_uploaded_file( $_FILES['file']['tmp_name'], $wp_upload_dir['path'] . '/' . $filename );
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$filename = $wp_upload_dir['path'] . '/' . $filename;
$attach_id = wp_insert_attachment( $attachment, $filename, 37 );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
$upload = wp_upload_bits( $_FILES['file']['name'], null, file_get_contents( $_FILES['file']['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');
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
update_post_meta( $post_id, '_thumbnail_id', $attach_id );