I am building a frontend posting mechanism. Everything is working fine besides the image uploader. I am using Wordpress Wysiwyg editor using <?php wp_editor ?>. When I click on the Add media button (which is part of the editor), the image is actually being uploaded to the previous post, because the current one is not existing yet.

Anyone has any idea how to solve this?



I am not sure this answers your question directly, but as technical trivia — WordPress actually creates a posts as soon as you open editor, with auto-draft status. This is being used precisely to have a target for things like attachments and custom field before it is actually saved by user.

So when user "saves" the post for the first times it's not actually being created, but existing auto-draft is getting changed into draft or published post.

You might have to either replicate this logic or rebuild related functionality, which relies on it.

  • Thanks! I already know this, and I was wondering if someone could help me with this as I am quite new to Wordpress development. I was also thinking perhaps to somehow pull the attachment after I create the post and set its parent post to the right one, but this is problematic. – nevos Jul 20 '14 at 11:59
  • I found this link that talks about the same issue, but I'm not sure how they solved it, can you maybe me help please? – nevos Jul 20 '14 at 12:01
  • @Nevos never did much of this (front-end editing), so not area I am thoroughly familiar with, sorry :) – Rarst Jul 20 '14 at 12:07

Here you go:

// Returns the attachment ID uploaded
$attach_id = omakUploadImage( $file, $post_id ); 

// Set an image as Featured Image to post with $post_id
// This is the cream you needed, Nivos
set_post_thumbnail( $post_id, $attach_id );

// omakUploadImage is a custom function which takes an input type="file" value

function omakUploadImage( $file, $post_id='' ) {

if( $post_id == '' ) {
    global $post;
    $post_id = $post->ID;

// Get Upload Directory
$uploaddir = wp_upload_dir();

/*  echo '<br/><br/>';print_r($_FILES); echo '<br/><br/>';print_r($file); */

// Create File Name 
$uploadfile = $uploaddir['path'] . '/' . basename( $file['name'] );
// Create Upload Path, Not Required though
$uploadpath = $uploaddir['path'] . '/';

echo '<br/><br/>';echo 'upload file: ';print_r($uploadfile);    
echo '<br/><br/>';echo 'upload path: ';print_r($uploadpath);

// Move Temp Named File to Actual File Name in UploadDir
$moved = move_uploaded_file( $file['tmp_name'] , $uploadfile );

// Get The Image Name
$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, $post_id );
//echo '<script>alert("AttachId: '.print_r($attach_id).'")</script>';

return $attach_id;

Hope it helps! There are some testing codes, which are commented out.

Here is where I have used it to implement front-end posting. http://www.omaksolutions.com/clients/neetinpuri/localmag/submit-your-business

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.