-
-
Save tommcfarlin/f6cdbafe6e5e9ed28f9b to your computer and use it in GitHub Desktop.
[WordPress] Specifying custom error codes when using Ajax in WordPress.
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
<?php | |
add_action( 'wp_ajax_retrieve_information', 'acme_retrieve_information' ); | |
add_action( 'wp_ajax_nopriv_retrieve_information', 'acme_retrieve_information' ); | |
/** | |
* Retrieves information for the user based on the specified post ID, | |
* the user ID, and whether or not the user has currently voted on this | |
* particular post. | |
* | |
* If information is not properly provided via the client-side, then one | |
* of the follow error messages will be sent back to the browser: | |
* | |
* - '-1' means that the post ID was not specified | |
* - '-2' means that the user ID was not specified | |
* - '-3' means that the user has already voted | |
* | |
* Otherwise, the information will be retrieved and the value of said | |
* information will be sent to the JavaScript. | |
* | |
* @since 1.0.0 | |
*/ | |
function acme_retrieve_information() { | |
if ( ! isset( $_GET['post_id'] ) ) { | |
$error = new WP_Error( '-1', 'The post ID was not set' ); | |
wp_send_json_error( $error ); | |
} | |
if ( ! isset( $_GET['user_id'] ) ) { | |
$error = new WP_Error( '-2', 'The user ID was not set' ); | |
wp_send_json_error( $error ); | |
} | |
if ( ! acme_get_user_has_voted( $post_id, $user_id ) ) { | |
$error = new WP_Error( '-3', 'The user has not voted' ); | |
wp_send_json_error( $error ); | |
} | |
/* If we reach this point, then we have everything we need in order | |
* to get the information the user is requesting. We can invoke the | |
* necessary function, store it in a variable, then echo it back to | |
* the user. | |
* | |
* Note this particular function's implementation is left out as it's | |
* irrelevant to the point being made in the code. | |
* | |
*/ | |
$information = acme_get_user_information( $post_id, $user_id ); | |
if ( ! $information ) { | |
$error = new WP_Error( '001', 'No user information was retrieved.', $information ); | |
wp_send_json_error( $information ); | |
} | |
wp_send_json_success( $information ); | |
} |
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
;(function( $ ) { | |
'use strict'; | |
$(function() { | |
$.ajax({ | |
url: wpa_demo.ajax_url, | |
method: 'GET', | |
data: { action: 'retrieve_information' } | |
}).done(function( response ) { | |
if ( true === response.success ) { | |
response_json = $.parseJSON( response.data ); | |
// Handle the response.data here | |
} else { | |
// Otherwise, handle the errors or the messages that were provided by the server, here. | |
error_codes = response.data.codes; | |
error_messages = response.data.messages; | |
} | |
}); | |
}); | |
})( jQuery ); | |
--- | |
$.get( ajaxurl, { | |
action: 'acme_retrieve_information', | |
post_id: $( '#post_id.hidden' ).text(), | |
user_id: $( '#user_id.hidden' ).text() | |
}, function( response ) { | |
switch ( response ) { | |
case '-1': | |
// The post ID wasn't specified. Maybe it was empty in the hidden element, wasn't rendered in the template, etc. | |
break; | |
case '-2': | |
// The user ID wasn't specified. Maybe it was empty in the hidden element, wasn't rendered in the template, etc. | |
break; | |
case '-3': | |
// The user has already voted, so perhaps display a message saying such. | |
break; | |
default: | |
if ( response.success ) { | |
// Use response.data here. | |
} else { | |
// Otherwise, handle the errors or the messages that were provided by the server, here. | |
error_codes = response.data.codes; | |
error_messages = response.data.messages; | |
} | |
break; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment