copilot0910
copilot0910

Reputation: 431

Wordpress AJAX not returning custom author meta

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);
            }
        });     
    });
});

Upvotes: 2

Views: 391

Answers (2)

diggy
diggy

Reputation: 6828

You need to specify the WordPress AJAX url, right after dataType:

url: ajax_object.ajax_url,

To get the url as a var, you'll have to localize your script, example:

add_action( 'wp_enqueue_scripts', 'so16523111_wp_enqueue_scripts' );
function so16523111_wp_enqueue_scripts() {
    wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my_script.js', array( 'jquery' ), '20130513', true );
    // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}

Also, wrap the data to send as a data parameter...

data: {action:'student_meta_info',studentID:id},

... and use response to check if successful:

success: function(response)

Furthermore, you'll need to adjust the callback, and the ajax hooks to reflect the action:

add_action( 'wp_ajax_nopriv_student_meta_info', 'get_student_meta_info' );
add_action( 'wp_ajax_ajax_student_meta_info', 'get_student_meta_info' );

Lastly, you'll have to use jQuery in noconflict mode:

jQuery(document).ready(function($)

Upvotes: 4

juan.obando
juan.obando

Reputation: 209

In the functions.php file, you didn't attach the hook to the right function (is get_student_meta_info, not student_meta_info):

add_action( 'wp_ajax_nopriv_ajax_request', 'get_student_meta_info' );
add_action( 'wp_ajax_ajax_request', 'get_student_meta_info' );

In the JavaScript code (AJAX call):

var data_to_send: {
    action: 'ajax_request', // This is how you call it in the add_action hooks
    studentID: id,
    ...
}

$.ajax({
    data: data_to_sent,
    success: function(r) { ... }
});

Hope it helps.

Upvotes: 0

Related Questions