Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to send a POST request from WordPress admin Dashboard
(function(){
var request = wp.ajax.post('custom_action', {param1: 'uppercase this string pls'});
// on success
request.done( function ( res ) {
console.log( res );
});
// on fail
request.fail( function ( res ) {
console.log( res );
});
})();
<?php
// this function will be called when the request is triggered
function listen_for_ajax() {
if ( ! empty( $_POST['param1'] ) ) {
$upercased = strtoupper( $_POST['param1']);
wp_send_json_success( $upercased );
}
wp_send_json_error('no param');
}
// hook this function for admin actions
add_action( "wp_ajax_custom_action", "listen_for_ajax");
// for public actions use `nopriv_` before action name
add_action( "wp_ajax_nopriv_custom_action", "listen_for_ajax");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment