Last active
November 25, 2015 01:05
-
-
Save junrillg/89dae7da6d629168c77d to your computer and use it in GitHub Desktop.
Platypus WP Ajax Request Guide
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
============================================== | |
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