wordpress upload images in custom page
using ajax call you need to create function in functions.php
file.
your ajax call like:
$("#YourButtnID").click(function(){ var firstname = $('#fname').val(); // get all the fields (input) like this.. // and now for all the input type files. var identity = jQuery('#identity').prop('files')[0]; var residence = jQuery('#residenceID').prop('files')[0]; var selfie = jQuery('#selfie').prop('files')[0]; //creating formData and inserting data into it var formData = new FormData(); //action -> this will be your function name in functions.php formData.append('action','your_custom_function'); //now all the data .. appending formData.append('fname',firstname); //Your images formData.append('identity', identity); formData.append('residence', residence); formData.append('selfie', selfie); $.ajax({ type: "POST", url: ajaxurl, contentType: false, processData: false, data: formData, dataType: "JSON", success:function(data){ if(data['status']){ //All went well }else{ // something went wrong }, error:function(err){ // You might not have caught any exception } });
and now in your functions.php
file, create function to get all this done
<?phpfunction your_custom_function(){ // initial response is false by default $response = array ('status'=>false,'msg'=>'invalid code'); // you can have server side validation here if you want // i.e. if(!$_REQUEST['fname']) this means if this field is not sent.. then return relevant response if (!function_exists('wp_handle_upload')) { require_once(ABSPATH . 'wp-admin/includes/file.php'); } $yourDBTable = $wpdb->prefix.'yourDBTable'; $uploadedfile = $_FILES['identity']; $upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($uploadedfile, $upload_overrides); //after uploading to uploads DIR you will get url of your image here $id_url = $movefile['url']; $uploadedfile = $_FILES['residence']; $upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($uploadedfile, $upload_overrides); $resid_url = $movefile['url']; $uploadedfile = $_FILES['selfie']; $upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($uploadedfile, $upload_overrides); $selfie_url = $movefile['url']; $user_data = array( 'fname' => $_REQUEST['fname'], 'photo_identity' => $id_url, 'photo_selfie' => $selfie_url, 'photo_residence' => $resid_url ); try { $result = $wpdb->insert($yourDBTable,$user_data); $response['status']=true; } catch (Exception $e) { $response['msg']="Something went wrong"; } echo json_encode($response); wp_die();}