'. 'if(jQuery)'. '{'. 'jQuery.ajax({'. 'type : "GET",'. 'url : "'.admin_url( 'admin-ajax.php' ).'",'. 'data : '. '{ '. 'action : "ajax-hits-counter-increment",'. 'post_id : '.$post->ID. '}'. '});'. '}'. ''; } function ajax_hits_counter_increment() { if( !isset($_GET['post_id']) || empty($_GET['post_id']) ) { die( '0' ); } $post_id = intval( filter_var( $_GET['post_id'], FILTER_SANITIZE_NUMBER_INT ) ); if( empty($post_id) ) { die( '0' ); } $current_hits = get_post_meta( $post_id, 'hits', true ); if( empty($current_hits) ) { $current_hits = 0; } $current_hits++; update_post_meta( $post_id, 'hits', $current_hits ); die( strval( $current_hits ) ); } /////////////////////////////////////////////////////////////////////////////// function ajax_hits_counter_posts_table_column( $column ) { $column['hits'] = 'Hits'; return $column; } function ajax_hits_counter_posts_table_row( $column_name, $post_id ) { if( $column_name=='hits' ) { $current_hits = get_post_meta( $post_id, 'hits', true ); if( empty($current_hits) ) { $current_hits = 0; } echo( $current_hits ); } } function ajax_hits_counter_posts_table_sortable( $column ) { $column['hits'] = 'hits'; return $column; } function ajax_hits_counter_posts_table_orderby( $vars ) { if( isset($vars['orderby']) && $vars['orderby']=='hits' ) { $vars = array_merge( $vars, array( 'meta_key' => 'hits', 'orderby' => 'meta_value_num' ) ); } return $vars; } /////////////////////////////////////////////////////////////////////////////// add_filter('the_content', 'ajax_hits_counter_init', 100); /////////////////////////////////////////////////////////////////////////////// add_action( 'wp_ajax_nopriv_ajax-hits-counter-increment', 'ajax_hits_counter_increment' ); add_action( 'wp_ajax_ajax-hits-counter-increment', 'ajax_hits_counter_increment' ); /////////////////////////////////////////////////////////////////////////////// add_filter( 'manage_posts_columns', 'ajax_hits_counter_posts_table_column' ); add_filter( 'manage_posts_custom_column', 'ajax_hits_counter_posts_table_row', 10, 2 ); add_filter( 'manage_edit-post_sortable_columns', 'ajax_hits_counter_posts_table_sortable' ); add_filter( 'request', 'ajax_hits_counter_posts_table_orderby' ); ///////////////////////////////////////////////////////////////////////////////