Skip to content

Instantly share code, notes, and snippets.

@lasida
Last active November 10, 2017 08:45
Show Gist options
  • Save lasida/fdce84cae22f0032a07bcb0007668da7 to your computer and use it in GitHub Desktop.
Save lasida/fdce84cae22f0032a07bcb0007668da7 to your computer and use it in GitHub Desktop.
Ajax View Counter
$(window).load(function() {
$.ajax({
url : kontakin.ajax_url,
type : 'post',
data : {
action : 'add_kontakin_view',
post_id : postID
},
success : function( response ) {
window.location.replace(linkdirection);
}
});
});
function add_kontakin_view() {
$view = get_post_meta( $_POST['post_id'], 'kontakin_view', true );
$view++;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
update_post_meta( $_POST['post_id'], 'kontakin_view', $view );
echo $view;
}
die();
}
add_action( 'wp_ajax_nopriv_add_kontakin_view', 'add_kontakin_view' );
add_action( 'wp_ajax_add_kontakin_view', 'add_kontakin_view' );
wp_enqueue_script( 'kontakin-functions', get_template_directory_uri() .'/js/functions.js', array('jquery'), null, true );
wp_localize_script( 'kontakin-functions', 'kontakin', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'pixel' => get_field('kontakin_facebook_pixel_id','option')
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment