Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've seen a few questions similar to this but I can't find a solution to the issue I have.

<?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);
 });
 </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);
  exit; // this is required to return a proper result
}

When I call the HitCount function in my page it renders the following

<script type="text/javascript" >
 jQuery(document).ready(function($) {
var data = {
    action: 'LogHit_callback',
    PostId:'86476'
};
jQuery.post('http://www.test.com/wp-admin/admin-ajax.php', data);
  });
</script>

The direct link to the admin-ajax.php file works in a browser but the callback function is never called?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

You are making a new request to the server and you appear to be loading a page that is not loading WordPress core functions. I'd need to see your HitCount.php to confirm this but I can't think of another explanation.

Loading WordPRess files piecemeal is tricky and prone to breakage as core code changes. You can probably get it working by loading wp-load.php, wp-settings.php, or wp-blog-header.php. I believe the latter is the officially correct one but it will load theme functions you probably don't need. I don't know for sure about that because I haven't used any of those tricks since the AJAX API became available, which brings me to...

The safest thing to do is use the AJAX API, and that is what I recommend unless you really know WordPRess core files and load sequence very very well, and also need extremely quick response. The AJAX API loads a lot so it can be slow, though in personal experience I have never noticed a lag worth worrying about.

share|improve this answer
    
I've been trying to figure out the AJAX API but I'm going in circles.. I've updated the main question with the new code but now nothing happens.. at all? –  Gavin Mannion Jan 10 '13 at 15:33
    
Going to accept this as the answer and repost a question with my broken AJAX API plugin.. thanks for pointing me in the right direction –  Gavin Mannion Jan 11 '13 at 4:25
add comment

Two issues I see in your updated code-

The action you you pass in your js is LogHit, but the action you set in add_action is LogHit_callback: wp_ajax_nopriv_LogHit_callback. one of those has to change to match the other.

The HitCount function- where is that hooked to add that code to your page? Also within that code block: jQuery.post(ajaxurl..., on the front end ajaxurl won't be defined. If that gets directly injected into a page then you should set that via admin_url( 'admin-ajax.php' ). The preferred method is to put that in its own js file, enqueue it, then use wp_localize_script to set ajaxurl:

wp_enqueue_script(
    'my-ajax-request',
    plugin_dir_url( __FILE__ ) . 'js/your_script.js',
    array( 'jquery' )
);

wp_localize_script(
    'my-ajax-request',
    'MyAjax',
    array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
);

Then use MyAjax.ajaxurl in your js:

jQuery.post(MyAjax.ajaxurl, data, function(response) {
share|improve this answer
    
I'm sure I'm missing something very simple here, I had updated the hook calls already and I'm now hardcoding the ajaxurl to take out some of the complexity.. I've updated above to try make this as simple as possible –  Gavin Mannion Jan 10 '13 at 17:13
add comment

You need to load the whole wordPress stuff and in the old days you had to do an include "../../../wp-load.php" or something like that.

But, now, best practice is to use admin-ajax.php to handle all ajax requests. And you need to understand hooks to do it which I believe you do :)

I would suggest reading this article http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global for starters.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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