Get uploaded image and attach it to the new post

You have to use the javascript FormData object to upload files via ajax. A simple tutorial is provided here. In PHP the file will be available via the globals $_FILES and $_POST.


Additional to the tutorial the WordPress specific data “action” (and maybe “nonce”) have to be added to the formData object:

formData.append('action', 'add_your_action_here');
// if necessary
formData.append('nonce', 'add_your_nonce_here');