Advertisement
designbymerovingi

Award points for image link click

Sep 29th, 2014
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. add_action( 'wp_footer', 'insert_img_click_points' );
  2. function insert_img_click_points() {
  3.  
  4.     if ( ! is_user_logged_in() ) return;
  5. ?>
  6.  
  7. <script type="text/javascript">
  8. jQuery(function($) {
  9.  
  10.     $( 'a#link img' ).click(function(){
  11.  
  12.         $.ajax({
  13.             type       : "POST",
  14.             data       : {
  15.                 action    : 'points-for-image-click',
  16.                 token     : <?php echo wp_create_nonce( 'points-for-image-click' ); ?>
  17.             },
  18.             url        : ajaxurl
  19.         });
  20.  
  21.     });
  22.  
  23. });
  24. </script>
  25. <?php
  26. }
  27.  
  28. /**
  29.  * Ajax Call Handler
  30.  * Whenever triggered, the user ID given for $admin_id receives
  31.  * points.
  32.  * Requires users to be logged in for points to be awarded.
  33.  */
  34. add_action( 'wp_ajax_points-for-image-click', 'award_points_for_click' );
  35. function award_points_for_click() {
  36.  
  37.     // Security
  38.     check_ajax_referer( 'points-for-image-click', 'token' );
  39.  
  40.     // Make sure myCRED is enabled
  41.     if ( ! function_exists( 'mycred' ) ) die( 0 );
  42.  
  43.     // Point Type
  44.     $point_type = 'mycred_default';
  45.  
  46.     // User to receive points
  47.     $admin_id = 1;
  48.  
  49.     // Load myCRED
  50.     $mycred = mycred( $point_type );
  51.  
  52.     // Award points
  53.     $mycred->add_creds(
  54.         'image_click',
  55.         $author_id
  56.         1,
  57.         'Image click',
  58.         0,
  59.         '',
  60.         $point_type
  61.     );
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement