Right now, I’m trying to retrieve custom author meta using AJAX for when a user clicks on the author’s name.
What’s going on is that my AJAX is not returning anything.
I’ve tested the variable I’m sending, and that is filled correctly, but I am not getting any response from WordPress.
Any help would be appreciated.
To note, the PHP is in my functions.php
file where there is another function that creates and saves the custom author meta field that I need to access the info from
Thank you,
Hunter
PHP
add_action('wp_ajax_nopriv_ajax_request', 'student_meta_info');
add_action('wp_ajax_ajax_request', 'student_meta_info');
function get_student_meta_info()
{
$studentID = $_POST['studentID'];
$review = the_author_meta('review', $id);
$weightloss = the_author_meta('weightloss', $id);
$gained = the_author_meta('gained', $id);
$AuthMeta = array("review" => $review, "weightloss" => $weightloss, "gained" => $gained);
echo json_encode($AuthMeta);
exit;
}
jQuery
$(document).ready(function()
{
$('.author').click(function()
{
var id = $(this).attr('id');
$.ajax({
type: 'POST',
action: 'student_meta_info',
studentID: id,
dataType: 'json',
success: function(data)
{
var review = data.review;
var weightloss = data.weightloss;
var gained = data.gained;
alert(data);
alert(review);
alert(weightloss);
alert(gained);
}
});
});
});
You need to specify the WordPress AJAX url, right after
dataType
:To get the url as a var, you’ll have to localize your script, example:
Also, wrap the data to send as a data parameter…
… and use
response
to check if successful:Furthermore, you’ll need to adjust the callback, and the ajax hooks to reflect the action:
Lastly, you’ll have to use jQuery in noconflict mode:
In the functions.php file, you didn’t attach the hook to the right function (is
get_student_meta_info
, notstudent_meta_info
):In the JavaScript code (AJAX call):
Hope it helps.