Skip to content

Instantly share code, notes, and snippets.

@junrillg
Last active November 25, 2015 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junrillg/89dae7da6d629168c77d to your computer and use it in GitHub Desktop.
Save junrillg/89dae7da6d629168c77d to your computer and use it in GitHub Desktop.
Platypus WP Ajax Request Guide
==============================================
PHP FILE
==============================================
add_action( 'wp_ajax_sample_action', 'my_action_callback' );
add_action( 'wp_ajax_nopriv_sample_action', 'my_action_callback' );
function my_action_callback(){
$_POST['his_not_email'];
if ( email_exist( $_POST['his_not_email'] ) ) {
echo json_encode( array( 'success' => 'true' ) );
} else {
echo json_encode( array( 'success' => 'false' ) );
}
die();
}
add_action( 'wp_enqueue_scripts', 'this_script' );
function this_script(){
wp_enqueue_script( 'handle', 'htt://location.js', array(), false, true );
wp_localize_script( 'handle', 'sample', array(
'url' => admin_url( 'admin-ajax.php' )
))
}
==============================================
Javascript File
==============================================
$( '.submit' ).on( 'click', function(e){
e.prevetDefault();
var email = $( '.input-email' ).val();
$.ajax({
url: sample.url
type: 'POST',
dataType : 'json'
data: {
action : 'sample_action',
this_not_email : email
}
sucess: function(respond){
console.log( respond.sucess );
}
error: function(e){
console.log(e);
}
})
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment