title: cant attach image to post in wordpress
body: <p>i am trying to upload base64 image. but the image is not attaching to post. Though it gives attachid and post id</p>
  
  <p>i tried the wp_handle_sideupload()</p>
  
  <pre><code>  $data = $_POST['base_img'];
  
  list($type, $data) = explode(';', $data);
  list(, $data)      = explode(',', $data);
  //$data1 = base64_decode($data);
  
  $img             = str_replace( 'data:image/png;base64,', '', $data );
  $img             = str_replace( ' ', '+', $img );
  $data1         = base64_decode( $img );
  
  
    $upload_dir = wp_upload_dir();
  
  // @new
  $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
  
  $decoded = $data1;
  $filename = 'my-base64-image.png';
  
  $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
  
  // @new
  $image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
  
  //HANDLE UPLOADED FILE
  if( !function_exists( 'wp_handle_sideload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
  }
  
  // Without that I'm getting a debug error!?
  if( !function_exists( 'wp_get_current_user' ) ) {
    require_once( ABSPATH . 'wp-includes/pluggable.php' );
  }
  
  // @new
  $file             = array();
  $file['error']    = '';
  $file['tmp_name'] = $upload_path . $hashed_filename;
  $file['name']     = $hashed_filename;
  $file['type']     = 'image/png';
  $file['size']     = filesize( $upload_path . $hashed_filename );
  
  // upload file to server
  // @new use $file instead of $image_upload
  $file_return = wp_handle_sideload( $file, array( 'test_form' =&gt; false ) );
  
  
  //var_dump($file_return); exit;
  $filename = $file_return['file'];
  $attachment = array(
   'post_mime_type' =&gt; $file_return['type'],
   'post_title' =&gt; preg_replace('/\.[^.]+$/', '', basename($filename)),
   'post_content' =&gt; '',
   'post_status' =&gt; 'inherit',
   'guid' =&gt; $wp_upload_dir['url'] . '/' . basename($filename)
   );
   $post_id = wp_insert_post($attachment);
  $attach_id = wp_insert_attachment( $attachment, $filename );
  
  update_post_meta($post_id,'_thumbnail_id',$attach_id);
  set_post_thumbnail( $post_id, $attach_id );
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/OyCLm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OyCLm.png" alt="enter image description here"></a></p>
  
  <p>The media gallery appears broken and the fetaured image is not inserted.
  Why should i cant attach image to post though correct attach id an dpost id are sending</p>