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
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:
Hook that into the AJAX system:
Submit your request to
http://site/wp-admin/admin-ajax.php
and passmy_update_pm
as an argument when the Javascript makes a request.You can set
ajax_url
similarly to this from the Codex: