Created
February 27, 2016 17:48
-
-
Save mrbobbybryant/c0ff9a87a2ea36827860 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
$( document).ready( function() { | |
var userSubmitButton = document.getElementById( 'user-submit-button' ); | |
var adminAjaxRequest = function( formData, action ) { | |
$.ajax({ | |
type: 'POST', | |
dataType: 'json', | |
url: screenReaderText.adminAjax, | |
data: { | |
action: action, | |
data: formData, | |
submission: document.getElementById( 'xyq').value, | |
security: screenReaderText.security | |
}, | |
success: function(response) { | |
if ( true === response.success ) { | |
alert( 'this was a success' ); | |
} else { | |
alert( 'You Suck' ); | |
} | |
} | |
}); | |
}; | |
userSubmitButton.addEventListener( 'click', function(event) { | |
event.preventDefault(); | |
var formData = { | |
'name' : document.getElementById( 'user-name').value, | |
'email' : document.getElementById( 'user-email').value, | |
'question' : document.getElementById( 'user-entry-content').value, | |
'product' : document.getElementById( 'product').value | |
}; | |
adminAjaxRequest( formData, 'process_user_generated_post' ); | |
} ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there,
I am a newbie into wordpress, php , js/ ajax etc. I try to learn these things by reading, watching tutorials and practicing code. So I found your tutorial and went through it couple of time.
I have checked your video putting this code into live .
simply, I am trying to import some data via an API, and save some info into wordpress database.
So, before I connect to the source, I did some modification to your code. but it gives me error, some help would be appreciated
` // start the java script section
$( document).ready( function() {
var userSubmitButton = document.getElementById( 'user-submit-button' );
`
This is the function
function process_user_generated_post() { /*if ( ! empty( $_POST[ 'submission' ] ) ) { wp_send_json_error( 'Honeypot Check Failed' ); } if ( ! check_ajax_referer( 'user-submitted-question', 'security' ) ) { wp_send_json_error( 'Security Check failed' ); }*/ $question_data = array( 'post_title' => sprintf( '%s-%s-%s', sanitize_text_field( $_POST[ 'data' ][ 'name' ] ), //sanitize_text_field( $_POST[ 'data' ][ 'product' ] ), esc_attr( current_time( 'Y-m-d' ) ) ), 'post_status' => 'draft', //'post_type' => 'user_question', 'post_content' => sanitize_text_field( $_POST[ 'data' ][ 'name' ] ) ); $post_id = wp_insert_post( $question_data, true ); if ( $post_id ) { wp_set_object_terms( $post_id, sanitize_text_field( $_POST[ 'data' ][ 'name' ] ), 'products', true ); //update_post_meta( $post_id, 'contact_email', sanitize_email( $_POST[ 'data' ][ 'email' ] ) ); } wp_send_json_success( $post_id ); } add_action( 'wp_ajax_process_user_generated_post', 'process_user_generated_post' ); add_action( 'wp_ajax_nopriv_process_user_generated_post', 'process_user_generated_post' );