5

This AJAX function gets called on a click:

$.ajax({
            url: 'site/ajax.php',
            data: {id: $('section#single article input:last-of-type').attr('value'), cote: 'like'}
        }).done(function(html) {}

It passes the ID of the post (which is contained in a hidden input) for function get_post_meta() that is located in ajax.php. This is what AJAX.php looks like:

$cote = get_post_meta($_GET['id'], 'cote', true);
if($_GET['cote'] == 'like') {
    $newCote = $cote++;
    update_post_meta($_GET['id'], 'cote', $newCote);
} else {
    $newCote = $cote--;
    update_post_meta($_GET['id'], 'cote', $newCote);
}

It is suppose to update the custom post's "cote" field, but the problem is that function get_post_meta() is undefined in ajax.php. This is the error message:

Fatal error: Call to undefined function get_post_meta() in /home2/electro/public_html/beta... on line 2
8

If you load a file directly none of the WordPress functions will work. That is why you should nearly always use the AJAX API. The AJAX API solves this problem. Everything loads in WordPress context.

You would wrap your processing PHP is a function:

function my_ajax_cb_wpse_108143() {
    $cote = get_post_meta($_POST['id'], 'cote', true);
    if($_POST['cote'] == 'like') {
        $newCote = $cote++;
        update_post_meta($_POST['id'], 'cote', $newCote);
    } else {
        $newCote = $cote--;
        update_post_meta($_POST['id'], 'cote', $newCote);
    }
}

Hook that into the AJAX system:

add_action('wp_ajax_my_update_pm', 'my_ajax_cb_wpse_108143');
add_action('wp_ajax_nopriv_my_update_pm', 'my_ajax_cb_wpse_108143');

Submit your request to http://site/wp-admin/admin-ajax.php and pass my_update_pm as an argument when the Javascript makes a request.

var data = {
    action: 'my_update_pm',
    id: jQuery('section#single article input:last-of-type').attr('value'), 
            cote: 'like'
};
jQuery.post(ajax_url, data, function(response) {
    // whatever you need to do; maybe nothing
});

You can set ajax_url similarly to this from the Codex:

add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {
    if( 'index.php' != $hook ) return;  // Only applies to dashboard panel
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery'));
    // 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' ), 'we_value' => $email_nonce ) );
}
  • Thank you! Can you explain this please: Submit your request to site/wp-admin/admin-ajax.php and pass my_update_pm as an argument when the Javascript makes a request. -- What is admin-ajax.php? – user25893 Jul 29 '13 at 11:28
  • Is it like an endpoint for the API? – user25893 Jul 29 '13 at 11:29
  • admin-ajax.php is a file used by the WordPress Core AJAX system. All AJAX API requests should submit to that address. – s_ha_dum Jul 29 '13 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy