Craete custom form in your theme template/file and submit it, insert data in database and move uploaded file.
View/Template (HTML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <div id="vp-form"> <div id="feedback"></div> <form type="post" method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" id="customTailoringForm" enctype="multipart/form-data"> <label for="name">Name:</label> <input name="name" type="text" /> <label for="email">Email:</label> <input name="email" type="text" /> <label for="phone">Phone:</label> <input name="phone" type="text" /> <label for="address">Address:</label> <input name="address" type="text" /> <label for="address">Ref Image:</label> <input name="ref_img" type="file" /> <input type="hidden" name="action" value="addCustomTailoring"/> <input type="submit"> </form> </div> |
function.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | function addCustomTailoring(){ global $wpdb; $name = $_POST['name']; $phone = $_POST['phone']; $email = $_POST['email']; $address = $_POST['address']; $ref_img = $_FILES["ref_img"]["name"]; $ref_imgTemp = $_FILES["ref_img"]["tmp_name"]; if($ref_img==""){ $ref_img = "none"; } $filename ="none"; $fileInfo = wp_check_filetype(basename($_FILES['ref_img']['name'])); $fileExt = $fileInfo['ext']; if($fileExt=="jpg" || $fileExt=="jpeg" || $fileExt=="png" || $fileExt=="PNG" || $fileExt=="gif" || $fileExt=="GIF") { // This file is valid $filename = date('dmyhms').'-'.basename($_FILES['ref_img']['name']); $target_path = ABSPATH.'wp-content/themes/storefront/assets/images/custom-tailoring-img/'.$filename; move_uploaded_file($ref_imgTemp, $target_path); echo $ref_img; }else{ // Invalid file $msg[] = $fileExt.' is Invalid File.'; } if( $wpdb->insert( 'custom_tailoring', array( 'name' => $name, 'email' => $email, 'address' => $address, 'phone' => $phone, 'ref_img' => $filename )) === false){ echo 'Error'; }else{ echo "Customer '".$name. "' successfully added, row ID is ".$wpdb->insert_id; } die(); } add_action( 'admin_post_nopriv_addCustomTailoring', 'addCustomTailoring' ); add_action( 'admin_post_addCustomTailoring', 'addCustomTailoring' ); |