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.

My site cached by "wp-supper cache" plugin and "cloudflare.com"

So my php function to count post views working incorrectly.

I try to use ajax for it but I'm noob with JS code so I can not know where is wrong.

In functions.php I create a simple function:

add_action('template_redirect', 'ajax_activation');
function ajax_activation(){
   //optional 
   wp_enqueue_script( 
      'ajax_script', 
       get_template_directory_uri() . '/js/ajax.js', // path to your js file for ajax operations
       array( 'jquery' ), false
   );
   //end optional
   wp_localize_script( 
      'ajax_script', // the name of your global.js registered file
      'ajax_object', // name 
       array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) // you can add other items for example for using a translated string in javascript/jquery context
   ); 
}

add_action('wp_ajax_get_PostViews', 'get_PostViews');
add_action('wp_ajax_nopriv_get_PostViews', 'get_PostViews');

function get_PostViews() {
$id = isset( $_POST['id'] ) ? $_POST['id'] : false;
$count_key = 'post_views_count'; 
$count = get_post_meta($post_ID, $count_key, true);
if( empty($count) ){ $count = 1; } else { $count++; }
update_post_meta($post_ID, $count_key, $count);
}

Code in ajax.js file:

var postID = $(".view_detail").attr("id");
jQuery.ajax({
    type: "POST",
    url: ajax_object.ajaxurl, // this is the object you defined in function.php
    data: {
       action: 'get_PostViews', // the name of your function
       id: postID // you can store it in html attribute for an easy access like: jQuery(element).attr('id');
    },
    success: function (result) {
    }
});

The page.php in my theme:

<div id="<?php the_ID(); ?>" <?php post_class('view_detail'); ?>>

Please tell me how can I do it work? Thank you very much!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I've spotted some errors here:

jQuery.ajax({
     type: "POST",
     contentType: "application/json; charset=utf-8", // default: 'application/x-www-form-urlencoded; charset=UTF-8'. you can not set
     url: "http://localhost/wp-admin/admin-ajax.php", // if you have correctly enabled ajax in wp, you should use the object you set up with the url
     data: "{'action':'get_PostViews(" + idpost + ")'}", // you can use a PlainObject notation, so you don't need to double quoted. action property is the name of your function as you written in function.php
     success: function (result) {
       alert('Update Success!');
     }
});

Take a look here jQuery.ajax() . To use ajax within WordPress follow these steps:

  1. enable ajax functionality
  2. declare your function in function.php
  3. use javascript/jquery to send data to server and to listen data retrieved

Enabling ajax

The best way to accomplish that ( in my opinion ) is:

//File functions.php
    add_action('template_redirect', 'ajax_activation');
    function ajax_activation(){
       //optional 
       wp_enqueue_script( 
          'ajax_script', 
           get_template_directory_uri() . '/js/jquery.ajax.js', // path to your js file for ajax operations
           array( 'jquery' ), false
       );
       //end optional
       wp_localize_script( 
          'ajax_script', // the name of your global.js registered file
          'ajax_object', // name 
           array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) // you can add other items for example for using a translated string in javascript/jquery context
       ); 
    }

Declare function

//File functions.php
add_action('wp_ajax_get_PostViews', 'get_PostViews');
add_action('wp_ajax_nopriv_get_PostViews', 'get_PostViews');

function get_PostViews() {
    $id = isset( $_POST['id'] ) ? $_POST['id'] : false;
    // your code here
    wp_die(); // | die(); you need this to avoid trailing zero
}

jQuery/Javascript

$.ajax({
    type: "POST",
    url: ajax_object.ajaxurl, // this is the object you defined in function.php
    data: {
       action: 'get_PostViews', // the name of your function
       id: // you can store it in html attribute for an easy access like: jQuery(element).attr('id');
    },
    success: function (result) {

    }
});

I guess you are using this function for all posts in a loop, you can call ajax once to do the work for all posts. For example i want to retrieve with ajax the titles of my posts:

HTML

<html>
<!-- some stuff here -->
<h3 id="<?php echo get_the_ID(); ?>" class="spyhole"></h3> <!-- there are many of this :  ) -->
<!-- some stuff here -->
</html>

jQuery

ids = [];
items = $('.spyhole');
$.each( items, function( i, v){
    ids.push( $(v).attr( 'id' ) ); // each value is added to array
});
$.ajax({
    type: "POST",
    url: ajax_object.ajaxurl,
    data: {
      action: 'getMyTitleAjax',
      id: ids
    },
    success: function (result) {
      data = $.parseJSON( result ); // Takes a well-formed JSON string and returns the resulting JavaScript object.
      $.each( data, function( i, v ){
        $('.spyhole[id="' + i + '"]').html( v ); // print the title
      });
    }
});

PHP

// Enabling ajax - functions.php
add_action('template_redirect', 'ajax_activation');
function ajax_activation(){
   //optional 
   wp_enqueue_script( 
      'ajax_script', 
       get_template_directory_uri() . '/js/jquery.ajax.js', // path to your js file for ajax operations
       array( 'jquery' ), false
   );
   //end optional
   wp_localize_script( 
      'ajax_script', // the name of your global.js registered file
      'ajax_object', // name 
       array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) // you can add other items for example for using a translated string in javascript/jquery context
   ); 
}

// Declare my function
add_action('wp_ajax_getMyTitleAjax', 'getMyTitleAjax', 3);
add_action('wp_ajax_nopriv_getMyTitleAjax', 'getMyTitleAjax', 3);
function getMyTitleAjax() {
    $ids = isset( $_POST['id'] ) ? $_POST['id'] : false; // check if there is something in global $_POST
    if( $ids && is_array( $ids ) ){
      foreach( $ids as $id ){
        $titles[$id] = get_the_title( $id ); 
      }
    }
    echo json_encode( $titles ); // prints the result
    wp_die(); // avoid trailing zero
}

Hope it helps, if something is not clear feel free to ask

Update

According to your question updates, change this:

function get_PostViews() {
$id = isset( $_POST['id'] ) ? $_POST['id'] : false;
$count_key = 'post_views_count'; 
$count = get_post_meta($post_ID, $count_key, true);
if( empty($count) ){ $count = 1; } else { $count++; }
update_post_meta($post_ID, $count_key, $count);
}

with this:

function get_PostViews() {
$id = isset( $_POST['id'] ) ? $_POST['id'] : false;
$count_key = 'post_views_count'; 
$count = get_post_meta($id, $count_key, true);
if( empty($count) ){ $count = 1; } else { $count++; }
update_post_meta($id, $count_key, $count);
}
share|improve this answer
    
Thanks @Emanuele - I will try again and tell you about the result. –  Zen Nguyễn Jul 24 '13 at 10:40
    
@ZenNguyễn, you're welcome. Yes, thank you –  iEmanuele Jul 24 '13 at 10:44
    
Hi @Emanuele, I was edit my Question again with your answer but still not working. I want to +1 view for current post & page, not use in archive loop. What's wrong I have again? Thank you very much! –  Zen Nguyễn Jul 24 '13 at 11:23
1  
Add update take a look, let me know if it works @ZenNguyễn –  iEmanuele Jul 24 '13 at 11:40
    
Thanks you, @Emanuele. I have a stupid problem! :D –  Zen Nguyễn Jul 24 '13 at 12:38

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.