I have a simple click counter that works using ajax, only problem is that if you're not logged in you can't update the counter meta box of the post.

In my case the post it's an ad that when clicked triggers the ajax function.

Is it possible to allow guest to update post meta on the front end using ajax?


This is the jQuery script:

jQuery(document).ready( function() {
    jQuery('.ad.<?php echo $post->ID; ?>').click(function() { 
        jQuery('#<?php echo $post->ID; ?>').submit();
    jQuery('#<?php echo $post->ID; ?>').submit(function(e) {
            type: "POST",
            url: ajaxurl,
            data: "action=adClickCounter&id="+<?php echo $post->ID?>,  
            success: function() {
                window.location.href = "http://www.example.com/";
                }, 50);

This is my click counter function:

function adClickCounter_ajax() {

$post_id = $_POST['id'];
$adClickCount = get_post_meta($post_id, '_adClickCount', true);
$adClickCount = $adClickCount + 1;

add_action('wp_ajax_adClickCounter', 'adClickCounter_ajax');
  • What does your server side code look? Jan 28, 2016 at 5:18
  • Please share your code logic that what you are trying and what is going wrong. Jan 28, 2016 at 5:34
  • Just update my entry. Sorry.
    – Al Rosado
    Jan 28, 2016 at 5:37

1 Answer 1


There are two hooks for adding AJAX handlers, you're missing the one for non-logged in visitors. You need:

add_action( 'wp_ajax_nopriv_adClickCounter', 'adClickCounter_ajax' );

... in addition to the add_action call you currently have. Note the extra nopriv_ in the tag.

  • Works like a charm. I will check all the codex documentation about wp_ajax, I spend many minutes looking for something like this. Guess didn't type the right question in google. Thanks.
    – Al Rosado
    Jan 28, 2016 at 17:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.