My usual ajax function looks like this:
function get_modal_playlist_classplan( $odv_video_id ) {
check_ajax_referer($_REQUEST['nonce'], "my_cool_nonce", false);
$result['type'] = "success";
$result['message'] = "Booooo";
if ( $some_test === true ) {
$result['message'] = "Yay";
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$result = json_encode($result);
echo $result;
} else {
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
die();
}
How do I replace my approach with the use of wp_send_json_success (and wp_send_json_error)?
I tried replacing $result['message'] = "yay"
with:
$return = array(
'message' => 'yay',
'some_other_data' => $_REQUEST['hello'] . ' world'
);
wp_send_json_success( $return );
But the ajax request fails.