I'm facing a weird issue with a Wordpress installation and I'm not sure how to proceed further. I've developed a simple AJAX endpoint only available for logged in users, which returns the details of a custom post type associated with the user. Example:
function get_return_request_detail() {
$returnRequestId = (isset($_POST['return_request_id'])) ? $_POST['return_request_id'] : '';
if(empty($returnRequestId)) {
echo json_encode("missing parameter");
wp_die();
}
$results = get_post_meta($returnRequestId);
$meta = unserialize($results["_meta"][0]);
if($results["_field_user_id"][0] == get_current_user_id()) {
... logic ...
echo json_encode($data);
} else {
echo json_encode("unauthorized");
}
wp_die();
}
add_action('wp_ajax_get_return_request_detail', 'get_return_request_detail');
The endpoint works fine and I retrieve the data correctly. An example on how I call it:
jQuery.ajax({
type: "post",
dataType: "json",
url: "/wp-admin/admin-ajax.php",
data: {
action:'get_return_request_detail',
return_request_id: id,
},
success: function(response) {
console.log(response);
},
});
The problem is that as soon as I switch to another page the whole front-end starts returning an error:
Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
I've tried enabling both debug and debug_log but I still receive the same message with nothing being written in the log. To enable it I've just added in the wp-config.php
:
define( 'WP_DISABLE_FATAL_ERROR_HANDLER', true );
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );
The weird behaviour is that, in order to make the front-end work again, I just need to visit an Admin page and the error disappears. My questions are:
- Is there any way to have a more detailed error log for this issue?
- Do you have any idea on what could be possibly causing it? Something related to admin-ajax?
What I've tried so far, with no results:
- Check Wordpress core file permission;
- Replace core files with a fresh installation;
- Followed this answer in order to enable error logging for admin-ajax.php (no results);
- Disable caching / security plugins;
- Disable Server Side caching (Dynamic Cache & Memcached);
- Check error logs on Server (the error produces no log);
Thanks
exit;
withwp-die();
in your AJAX function.wp_die()
and also withwp_send_json()
but the error keeps occurring