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!
Leave a Reply
You must be logged in to post a comment.
Hi, this is good. I need to know whether this is available as an add-on in the mycred plugin?
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.
I wonder if a similar thing could be done for posts, so that the shortcode in the template instead of on the post itself would grab the author’s ID ?
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”.
Hello there,
How can we create a “limit” for this like regular comment hook one?
Thank you.
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.
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
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.
Hello. How can I show the medals (ranks) beside user’s avatar?
It depends on where this user avatar is. Is this on a BuddyPress profile or comment section or some other place?
How to show serveral different give points button,
eg: give 1 ;give 2;
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.
Hey Gabriel,
is there a way to put this new “Like” myCRED feature in Activity’s BuddyPress page?
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:
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.
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
Check if you are getting a javascript error on the page. The button requires jQuery to be loaded on the page, no other requirements exists.
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?
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
disabled WTC completely, the button still doesn’t work.
Is the mycred-send.js file loaded on the page?
no, it seems it is not loaded, and I don’t have it in WTC minify either. I do have transfers add-on activated. what could cause it not to load?
No the mycred-send.js file is loaded by the core. Try downloading a fresh copy of 1.5.4 and replace your existing copy using FTP and see if the file gets loaded.
OK, I did that, but it’s the same.
still waiting for a solution. thank you
Sorry for the delay, been trying to replicate the issue. You could enqueue the script via your theme by using something like this: http://pastebin.com/mEUpMJvy.
added your functions to functions.php, but I’m afraid mycred-send.js is still not loaded, and the button is still not working.
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!
I have updated the code to take into account the circumstances where this error occurs. Just replace your current code with the new one.
Hi, and how to make that this button was seen only by the author of a post?
Hey. You would need to adjust the code to check the post the comment belongs to and if the current user is not the post author, hide the button.
Here is an updated version of the code used in this tutorial: http://pastebin.com/aCbwwGL1