Created
July 23, 2019 11:51
-
-
Save tylerzey/ab0a196f686458796f3f4b4131c67c52 to your computer and use it in GitHub Desktop.
wp ajax for sophia
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
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(); | |
} |
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.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