I'm making this front end post submission in wordpress with ajax and php and i'm having trouble uploading the feature image and the images for the portfolio sections
Every custom field save fine but when i integrated the code for upload the images it just send me error 400on admin-ajax in the console, i tried diferent things like $('#post_image')[0].files[0]; or $('#post_image').prop('files')[0]; but i get the same error
Right now im only testing with the featured image using this as a reference but i get the same 400 error
The ajax file i'm using is
$('#btn-click').on('click', function(){
var cat_servicios = $('.cat-servicios').find(':selected').val();
var nombre_empresa = $('#nombre_empresa').val();
var creacion_empresa = $('#creacion_empresa').val();
var nit_empresa = $('#nit_empresa').val();
var pagina_empresa = $('#pagina_empresa').val();
var no_empleados = $('input[name=no_empleados]:checked').val();
var post_image = $('#post_image').val();
var pd_empresa = $('#pd_empresa').val();
var tempresa = $('input[name=tempresa]:checked').val();
var cl_empresa = $('.cl_empresa').val();
var descripcion_empresa = $('#descripcion_empresa').val();
var certificaciones_empresa = $('#certificaciones_empresa').val();
var areas_empresa = $('#areas_empresa').val();
var dep_empresa = $('.dep-empresas').find(':selected').val();
var mun_empresa = $('.mun-empresas').find(':selected').val();
var direccion_empresa = $('#direccion_empresa').val();
var nombre_contacto = $('#nombre_contacto').val();
var correo_elec1 = $('#correo_elec1').val();
var correo_elec2 = $('#correo_elec2').val();
var nro_tlf1 = $('#nro_tlf1').val();
var nro_tlf2 = $('#nro_tlf2').val();
var post_image = $('#post_image').prop('files')[0];
$.ajax({
url : ajax_object.ajaxurl,
type:'POST',
contentType: false,
processData: false,
data: {
action:'themedomain_post_if_submitted',
cat_servicios:cat_servicios,
nombre_empresa:nombre_empresa,
creacion_empresa:creacion_empresa,
nit_empresa:nit_empresa,
pagina_empresa:pagina_empresa,
no_empleados:no_empleados,
pd_empresa:pd_empresa,
post_image:post_image,
tempresa:tempresa,
cl_empresa:cl_empresa,
descripcion_empresa:descripcion_empresa,
certificaciones_empresa:certificaciones_empresa,
areas_empresa:areas_empresa,
dep_empresa:dep_empresa,
mun_empresa:mun_empresa,
direccion_empresa:direccion_empresa,
nombre_contacto:nombre_contacto,
correo_elec1:correo_elec1,
correo_elec2:correo_elec2,
nro_tlf1:nro_tlf1,
nro_tlf2:nro_tlf2,
post_image:post_image,
},
success: function(response){
$(".success_msg").css("display","block");
$("#new_post").hide();
}, error: function(data){
$(".error_msg").css("display","block");
}
});
});
and the code php is for the file is
if ( isset( $_FILES['post_image'] ) ) {
$attach_id = media_handle_upload( 'post_image', $post_id );
if ( $attach_id && ! is_wp_error( $attach_id ) ) {
set_post_thumbnail( $post_id, $attach_id );
}
}
post_image is the name of the input file
Im very new to ajax so maybe there is something that i just skipped or don't comprend yet
Thanks for the help!