I've created a tiny AJAX plugin to count hits on my articles and bypass caching but it just isn't working and not throwing up any errors.
Can you see what I am doing wrong here?
<?php
add_action('wp_ajax_nopriv_LogHit_callback', 'LogHit_callback');
add_action('wp_ajax_LogHit_callback', 'LogHit_callback');
function HitCount() {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
action: 'LogHit_callback',
PostId:'<?php echo get_the_ID() ?>'
};
jQuery.post('http://www.test.com/wp-admin/admin-ajax.php', data, function( r ) {
alert( r );
});
</script>
<?php
function LogHit_callback() {
global $wpdb; // this is how you get access to the database
$postId = $_POST['PostId'];
$hits = get_post_meta((int)$postId, 'hit-counter', true);
$hits = $hits + 1;
update_post_meta((int)$postId, 'hit-counter', (int)$hits);
return $postId;
exit; // this is required to return a proper result
}
}
It writes the following into my HTML where I call the HitCount function
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
action: 'LogHit_callback',
PostId:'86505'
};
jQuery.ajax('http://www.lazygamer.net/wp-admin/admin-ajax.php', data, function( r ) {
alert( r );
});
</script>
I know hard coding the URL to admin-ajax is bad but I am trying to rule out issues. if I put an alert inside the ajax call that is written to the html it works so I know that is being called.
But the second alert in the plugin file never gets called?
}
missing afteralert( r );