Created
November 7, 2016 08:44
-
-
Save vishnusomanus/3db2e295aa112793b3aa508f472889dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- jQuery --> | |
<script type="text/javascript"> | |
jQuery('form.portfolio_form').on('submit', function (e) { | |
e.preventDefault(); | |
var name = jQuery('form.portfolio_form .name').val(); | |
var tag = jQuery('form.portfolio_form .tag').val(); | |
var link = jQuery('form.portfolio_form .link').val(); | |
var formData = new FormData(jQuery('form.portfolio_form')[0]); | |
jQuery.ajax({ | |
cache: false, | |
processData: false, | |
contentType: false, | |
data: formData, | |
type: 'post', | |
url: '/wp-admin/admin-ajax.php', | |
success: function (data) { | |
console.log(data); | |
jQuery('.contact_us_form .msg').css('display','block'); | |
jQuery('.contact_us_form .msg').html('Portfolio added successfully.'); | |
} | |
}); | |
}); | |
</script> | |
<!-- function.php wp insert --> | |
<?php | |
add_action( 'wp_ajax_form_submit', 'form_submit' ); | |
add_action( 'wp_ajax_nopriv_form_submit', 'form_submit' ); | |
function form_submit() { | |
$my_post = array( | |
'post_title' => wp_strip_all_tags( $_POST['name'] ), | |
'post_status' => 'draft', | |
'post_author' => 1, | |
'post_type' => 'tie-portfolio' | |
); | |
$post_id = wp_insert_post( $my_post ); | |
update_post_meta($post_id, 'wpcf-website-url', $_POST['link']); | |
update_post_meta($post_id, 'wpcf-tag-line', $_POST['tagline']); | |
if (!function_exists('wp_generate_attachment_metadata')){ | |
require_once(ABSPATH . "wp-admin" . '/includes/image.php'); | |
require_once(ABSPATH . "wp-admin" . '/includes/file.php'); | |
require_once(ABSPATH . "wp-admin" . '/includes/media.php'); | |
} | |
//var_dump(function_exists('wp_generate_attachment_metadata')); | |
//die('test'); | |
if (isset($_FILES['image'])) { | |
$file = 'image'; | |
// foreach ($_FILES as $file => $array) { | |
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) { | |
return "upload error : " . $_FILES[$file]['error']; | |
}else{ | |
$attach_id = media_handle_upload( $file, $post_id ); | |
set_post_thumbnail( $post_id, $attach_id ); | |
//die('upload'); | |
} | |
//}foreach | |
} | |
wp_die(); | |
} | |
?> | |
<form name="portfolio_form" action="" method="post" class="portfolio_form" enctype="multipart/form-data"> | |
<input type="hidden" value="form_submit" name="action"> | |
<input type="text" placeholder="Company Name" name="name" class="name"> | |
<input type="text" placeholder="Tag line" name="tagline" class="tag"> | |
<input type="text" placeholder="Website link" name="link" class="link"> | |
<input type="file" name="image" id="image_part" accept="image/x-png, image/gif, image/jpeg"> | |
<img id="image_preview"></img> | |
</fieldset> | |
<input type="submit" value="submit" > | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment