Skip to content

Instantly share code, notes, and snippets.

@tylerzey
Created July 23, 2019 11:51
Show Gist options
  • Save tylerzey/ab0a196f686458796f3f4b4131c67c52 to your computer and use it in GitHub Desktop.
Save tylerzey/ab0a196f686458796f3f4b4131c67c52 to your computer and use it in GitHub Desktop.
wp ajax for sophia
add_action('wp_ajax_testAction', 'testActionFunction');
add_action('wp_ajax_nopriv_testAction', 'testActionFunction'); // only include if non-logged in users should be able to do this
function testActionFunction(){
//You can catch variables like this
$name = !empty($_POST['name'])?$_POST['name']:'';
// You return data by echo it or by wp_send_json
echo $name;
wp_die();
}
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
dataType: 'json',
method: 'POST',
async: false,
data: {
// action is required to match
action: 'testAction',
// include any extra data here like the name / email below
name: 'tyler',
email: 'Tylerzey@gmail',
},
success: result => {
console.warn(result);
},
error: error => {
console.error(error);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment