PHP WordPress JS
WordPess - Add Custom Ajax Endpoints
396 days agoAdds a custom WP AJAX endpoint.
<?php
// Action for unauthorized users
add_action('wp_ajax_nopriv_my_action','my_action');
// Action for authorized users
add_action('wp_ajax_my_action', 'my_action');
public function my_action() {
echo "test";
}
?>
<script>
jQuery.ajax({
type: "GET",
dataType: "JSON",
url: '/wp-admin/admin-ajax.php',
data: {
action: "my_action",
},
error: function(response, error) {
console.log("wrong");
},
success : function(response) {
if(response.type === "success") {
console.log("Success");
}
}
});
</script>
