trying to use wp_handle_upload with ajax

I’ve used this tutorial to build an option page form with Ajax.

Now, i want to use the wp_handle_upload to upload an image.
i tried this http://pastebin.com/35HW8RSZ but with no success.

Read More

help will be appreciated.
Asaf.

Related posts

Leave a Reply

2 comments

  1. You need to include a few files to get wp_handle_upload to work, and i have a function just for that :

    function My_wp_handle_upload($file_handler,$overrides) {
    
      // check to make sure its a successful upload
      if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
    
      require_once(ABSPATH . "wp-admin" . '/includes/image.php');
      require_once(ABSPATH . "wp-admin" . '/includes/file.php');
      require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
      $upload = wp_handle_upload( $file_handler, $overrides );
      return $upload ;
    }
    

    so after you add this function to your file just change your call from :

    $file = wp_handle_upload($data['MoobLogoUpload'], $overrides);
    

    to:

    $file = My_wp_handle_upload($data['MoobLogoUpload'], $overrides);
    

    Update

    Just remembered that you must use an ajax upload solution like:

    • JQurey form plugin
    • Uplodify
    • SWFUpload

    any other or submit by iframe since it is not possible to upload files using the browser’s XMLHttpRequest object