WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] AJAX Click Counter - How to Update WP Database? (3 posts)

  1. DaHermitLife
    Member
    Posted 2 years ago #

    Hi, I'm trying to write a plugin which will count clicks to the database for any link the following code is added to:
    onclick="myCounter(<?php echo $post->ID ?>);"

    I've read the Codex on AJAX in Plugins, as well as all other posts I could find about this online, but I just can't seem to get it to work. Anyone familiar with AJAX use in WP, please let me know what I'm doing wrong. Here is my code so far...

    In my plugin folder I have /js/Counter.js:

    function myCounter(postid){
      jQuery.post(
        MyAjax.ajaxURL,{
          action:'myClickCounter',
          id:postid
          }
        )
    };

    In my plugin file /referrer-hits.php I have this:

    // embed the javascript file that makes the AJAX request
    wp_enqueue_script( 'Counter', plugin_dir_url( __FILE__ ) . 'js/Counter.js', array( 'jquery' ) );
    
    // declare the URL to the file that handles the AJAX request
    wp_localize_script( 'Counter', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    
    function myClickCounter() {
    	global $wpdb;
    	$post_id = $_POST['id'];
    	$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = meta_value+1 WHERE post_id = %d AND meta_key = 'outgoing_hits'", $post_id ) )
    	or die("Invalid query: " . mysql_error());
    }
    add_action('wp_ajax_myClickCounter', 'myClickCounter' );
    add_action('wp_ajax_nopriv_myClickCounter', 'myClickCounter' );

    I've found several articles on how to pass a variable to a separate php file via POST with AJAX, but I can't figure out how to pass it to my plugin code (if that makes sense). Any help appreciated on this!

    [ Please don't bump, that's not permitted here. ]

  2. DaHermitLife
    Member
    Posted 2 years ago #

    I was finally able to fix the problem, so will share my updated working code for anyone who may find it useful.

    "link-out" is added to any post's link where counting outgoing clicks is needed. For example:

    <a href="<?php echo $link; ?>" link-out="<?php echo $post->ID ?>">

    It could be added to any link on the site, but for my purposes I'm counting links associated with a post ID. jQuery grabs the postID contained in "link-out" and sends it to the php file for database update when clicked.

    Code inside .js file:

    jQuery(document).ready(function($) {
    
    	$("a[link-out]").click(function() {
        	var linkout = $(this).attr("link-out");
        	var data = {
    			action: 'my_action',
    			postid: linkout
          	};
        	$.post(MyAjax.ajaxurl, data);
      	});
    
    });

    Code in plugin file .php:

    // embed the javascript file that makes the AJAX request
    wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/countclicks.js', array( 'jquery' ) );
    // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    
    add_action('wp_ajax_my_action', 'my_action_callback');
    add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
    
    function my_action_callback() {
    	global $wpdb;
    	$post_id = $_POST['postid'];
    	$post_id = mysql_real_escape_string($post_id);
    	$wpdb->query("UPDATE wp_postmeta SET meta_value = meta_value+1 WHERE post_id = '$post_id' AND meta_key = 'clicks_out'");
    }
  3. odddic
    Member
    Posted 1 year ago #

    Hay @DaHermitLife,

    Are you success to make the click counter plugin? Could you like to share to me? Someday I spent with nothing :(

    I'll buy a beer for you ^^

    Thanks,
    odddic

Topic Closed

This topic has been closed to new replies.

About this Topic