Comment appreciation with points

Home / Tutorials / Comment appreciation with points

Navigation:

  1. Description
  2. Requirements
  3. Hooking into Comments
  4. Done

Description

In this tutorial we will add the mycred_send shortcode to your posts comments to allow users to give points to the comment author as a form of “Appreciation”.

Requirements

  • This tutorial requires myCRED version 1.1 or higher.

Hooking into Comments

For this tutorial, we will be hooking into the comment text using the comment_text filter and append the mycred_send shortcode to the end of the text which will generate a simple button that users can click on to send 1 point to the comment author.

First, lets construct our custom filter:

add_filter( 'comment_text', 'my_custom_comment_text', 10, 2 );
function my_custom_comment_text( $comment_text, $comment )
{
	return $comment_text;
}

The WordPress codex tells us that the comment_text filter has two variables. The comment text itself and the comment object. Since we need to get the comment authors ID, we will need to call both of these variables.

The first thing our filter needs to do is to make sure that the current user is logged in, that the comment author is a registered member and that this is not the admin area. If this is not the case, we will return the text without the shortcode:

function my_custom_comment_text( $comment_text, $comment )
{
	// Make sure the current user is logged in
	if ( !is_user_logged_in() || $comment->user_id == 0 || is_admin() )
		return $comment_text;

	return $comment_text;
}

Note that if we do not include the is_admin() check, the appreciation button will be visible in the comment editor screen as well!

Next up we will load the myCRED Settings Object and the current users ID. Once we have these we will need to make sure that neither the current user or the comment authors are “excluded” and that these two are not the same:

function my_custom_comment_text( $comment_text, $comment )
{
	// Make sure the current user is logged in
	if ( !is_user_logged_in() || $comment->user_id == 0 || is_admin() )
		return $comment_text;

	// Prep
	$mycred = mycred();
	$cui = get_current_user_id();

	// Make sure the current user is not the comment author
	if ( $cui == $comment->user_id ) return $comment_text;

	// Make sure the current user is not excluded
	if ( $mycred->exclude_user( $cui ) ) return $comment_text;

	// Make sure the comment author is not excluded
	if ( $mycred->exclude_user( $comment->user_id ) ) return $comment_text;

	return $comment_text;
}

Finally we will include the mycred_shortcode. Feel free to change the shortcode variables to something that suits you. In the below example, users will be able to send 1 point to the comment author and we call this action “Comment Appreciation”:

add_filter( 'comment_text', 'my_custom_comment_text', 10, 2 );
function my_custom_comment_text( $comment_text, $comment )
{
	// Make sure the current user is logged in
	if ( !is_user_logged_in() || $comment->user_id == 0 || is_admin() )
		return $comment_text;

	// Prep
	$mycred = mycred();
	$cui = get_current_user_id();

	// Make sure the current user is not the comment author
	if ( $cui == $comment->user_id ) return $comment_text;

	// Make sure the current user is not excldued
	if ( $mycred->exclude_user( $cui ) ) return $comment_text;

	// Make sure the comment author is not excluded
	if ( $mycred->exclude_user( $comment->user_id ) ) return $comment_text;

	// Prep shortcode details
	$user_id = $comment->user_id;
	$amount = 1;
	$label = 'Give 1 Point';
	$ref = 'comment_appreciation';
	$entry = 'Comment Appreciation';

	// Append shortcode to the end of the comment text
	$add_points = '[[mycred_send to="' . $user_id . '" amount="' . $amount . '" log="' . $entry . '"]' . $label . '[/mycred_send]]';
	$comment_text .= '
<div class="comment-like">' . do_shortcode( $add_points ) . '</div>';

	// Return result
	return $comment_text;
}

Done

And that’s it. Place the code in your themes functions.php file and upload! Remember that if you are using an account that is excluded or if you are the comment author you will not be able to do any Comment Appreciation!

    1. Hey Sam.

      This is a tutorial, so this is not included in myCRED, it shows you as an example off how you can use myCRED. The above code goes in your themes functions.php file, just like any other customization.

    1. Of course you can. The mycred_send shortcode can be used anywhere and as long as you use it inside the Loop, you can nominate to give the points to the post author by setting the to attribute to “author”.

    1. Hey Martin.

      There is no limit option for the mycred_send shortcode which this tutorial uses. The easiest way would be for you to copy the code that renders this shortcode (located in mycred/includes/mycred-shortcodes.php) and paste it into a custom shortcode you create via your themes functions.php file.

      If you feel uncomfortable with PHP, you can always contact me via the Contact form here on mycred.me and I can provide further assistance.

  1. Hello there!

    I have just copied the code inside the functions.pho file and I got a fatal error :
    “Fatal error: Cannot redeclare my_custom_comment_text() (previously declared in /hermes/waloraweb084/b2770/moo.jauntracecom1/wordpress/wp-content/themes/spacious/functions.php:113) in /hermes/waloraweb084/b2770/moo.jauntracecom1/wordpress/wp-content/themes/spacious/functions.php on line 125”

    Do you have any ideas? I am very new on this and i don’t know if i am doing correctly!

    Thank you

    1. Hi.

      As the error message states, you are using a code twice. In PHP every function must be unique so when you have two functions with the same name, the system crashes. So make sure you only copy the code once by checking the file mentioned in your error message on the lines given.

    1. Hi.

      Simply add the mycred_send shortcode multiple times each with it’s own amount. So one with amount=”1″ and another with amount=”2″ etc. You can use the mycred_send shortcode as many times as you want.

    1. Sure, you could add a BuddyPress Activity for the comment appreciation. Just make sure you do not award points for BuddyPress profile updates since then you will also award points for this activity insertion and cause an infinite loop.

      The following script will add in a new activity for “comment_appreciations”. If you changed the reference then this need to be reflected int his code:

      add_filter( 'mycred_add', 'mycred_pro_share_in_bp', 20, 3 );
      function mycred_pro_share_in_bp( $reply, $request, $mycred ) {
      
      	if ( $reply === false || $request['ref'] != 'comment_appreciation' ) return $reply;
      
      	if ( function_exists( 'bp_activity_add' ) )
      		bp_activity_add( array(
      			'user_id'      => (int) $request['user_id'],
      			'content'      => $request['entry'],
      			'primary_link' => bp_core_get_userlink( (int) $request['user_id'], false, true ),
      			'component'    => buddypress()->activity->id,
      			'type'         => 'comment_appreciation',
      		) );
      
      	return $reply;
      
      }
      
  2. Hey Gabriel,
    excuse me, but i don’t understand where i have to put this code.
    I’ve tried in theme functions.php, but nothing.
    I’m looking for something similar “Facebook Like” or similar default buddypress favorite button.

  3. I have jquery, but this button is not working. nothing happens when I press it. weird thing is that it worked for a while, and now it doesn’t. I use WTC
    what scripts does this rely on and maybe it shouldn’t be minified?

    thanks

  4. I’m not getting anything that could cause this, only two errors about other plugins, but they are local, only belong to the plugin. Jquery is loading in the header, and its not minified.
    but for some reason, it’s loading wp-includes/js/jquery/jquery.js?ver=1.11.1, which I believe is an old version? could this be the cause?

  5. after loading the latest version from Google, //ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js, the button still doesn’t work, so I guess this is not related to Jquery

    1. added your functions to functions.php, but I’m afraid mycred-send.js is still not loaded, and the button is still not working.

  6. Hey, Gabriel !
    After clicking post comment button, i can see only these warnings:
    Warning: Missing argument 2 for my_custom_comment_text() in /home/user6347/rc/wp/wp-content/themes/tonic/functions.php on line 815

    Warning: Cannot modify header information – headers already sent by (output started at /home/user6347/rc/wp/wp-content/themes/tonic/functions.php:815) in /home/user6347/rc/wp/wp-includes/pluggable.php on line 1178

    What is my fault ?
    Thank you!

Leave a Reply

Last edited October 28, 2016