Plugin authors can now tie directly into CubePoints Buddypress Integration and add options to the admin menu. Such as setting values for earning/deducting points. You can also log the actions.
Let’s get started. I’ll explain the best I can what these functions do.
cp_points($type, $uid, $points, $data)
- $type: Your unique action identifier (It identifies the purpose of the point)
- $uid: the user id to which points should be given
- $points: how many points you want to give
- $data: Don’t worry about this one. We can leave it blank. I can be used to show more detailed info in the database if you’d like to run queries on it.
- Not sure how to fully use this since there is no documentation. But if you place something in there it won’t show on the front end in the logs. Only the database.
Here is the function you need to add to your action to give/remove points.
function give_point_on_something_happened(){ global $bp; cp_points("hello_something_happened",$bp->loggedin_user->id, get_option("points_for_my_something_happened_action"),""); }
Next thing you need to do it log it correctly. Here is the code block:
// your_do_action_here is a do_action you need to find. give_point_on_something_happened is the same function name as above. add_action('your_do_action_here','give_point_on_something_happened'); // Log Action add_action('cp_logs_description', "my_hello_something_happened_log_message",10,4); // 4 is for # of args function my_hello_something_happened_log_message($type,$uid,$points,$data){ if($type!="hello_something_happened") // This is same as we used in cp_points return; echo "The wonderful action happened"; // This is the text that shows in the logs. So make sure it makes sense according to the action they did to earn/remove points. }
Last thing to do populate the admin menu of CubePoints Buddypress Integration so admins can change the point values for actions.
Above in the give_point_on_something_happened() function we had this:
get_option(“points_for_my_something_happened_action”)
So make sure you use the same text. In this example it’s points_for_my_something_happened_action
Here is the code for building the admin menu inside of my plugin.
<?php // Admin Menu for CubePoints Buddypress Integration function example_cp_update_settings(){ update_option('points_for_my_something_happened_action', intval($_POST['points_for_my_something_happened_action'])); } add_action("bp_cubepoint_settings_updated","example_cp_update_settings"); function example_show_cp_settings(){ ?> <tr> <th colspan="3"><h4 align="center"><?php _e('Below Requires','cp_buddypress'); ?> <a href="http://www.yousite.com/yourpluginURL">Plugin Name</a> [YourSite.com]</h4></th> </tr> <tr valign="top"> <th scope="row"><label for="points_for_my_something_happened_action"><?php _e('Points for this action','cp_buddypress'); ?>:</label></th> <td valign="middle"><input type="text" id="points_for_my_something_happened_action" name="points_for_my_something_happened_action" value="<?php echo get_option('points_for_my_something_happened_action'); ?>" size="2" /></td> <td><input type="button" onclick="document.getElementById('points_for_my_something_happened_action').value='0'" value="<?php _e('Do not add points','cp_buddypress'); ?>" class="button" /></td> </tr> <?php } add_action("bp_cubepoint_main_settings","example_show_cp_settings"); ?>
Here is the whole required code all placed together along with some other things I didn’t mention above so it’s optional. For example the Point Blocker feature which isn’t required but handy if you need to ban people from earning points for a bit.
<?php if(!function_exists("cp_ready")) { return; // prevent any crash } // Add Points for this awesome action function give_point_on_something_happened() { global $bp; $bpcpspamlist = explode(',' , get_option( 'bp_spammer_cp_bp' ) ); foreach ( $bpcpspamlist as $spammer_id ) { if ($bp->loggedin_user->id == $spammer_id ) { $is_spammer = true; break; } else { $is_spammer = false; } } if ($is_spammer == false) { cp_points("hello_something_happened",$bp->loggedin_user->id, get_option("points_for_my_something_happened_action"),""); } } // your_do_action_here is a do_action you need to find. give_point_on_something_happened is the same function name as above. add_action('your_do_action_here','give_point_on_something_happened'); // Log Action add_action('cp_logs_description', "my_hello_something_happened_log_message",10,4); // 4 is for # of args function my_hello_something_happened_log_message($type,$uid,$points,$data){ if($type!="hello_something_happened") // This is same as we used in cp_points return; echo "The wonderful action happened"; // This is the text that shows in the logs. So make sure it makes sense according to the action they did to earn/remove points. } // Admin Menu for CubePoints Buddypress Integration function example_cp_update_settings(){ update_option('points_for_my_something_happened_action', intval($_POST['points_for_my_something_happened_action'])); } add_action("bp_cubepoint_settings_updated","example_cp_update_settings"); function example_show_cp_settings(){ ?> <tr> <th colspan="3"><h4 align="center"><?php _e('Below Requires','cp_buddypress'); ?> <a href="http://www.yousite.com/yourpluginURL">Plugin Name</a> [YourSite.com]</h4></th> </tr> <tr valign="top"> <th scope="row"><label for="points_for_my_something_happened_action"><?php _e('Points for this action','cp_buddypress'); ?>:</label></th> <td valign="middle"><input type="text" id="points_for_my_something_happened_action" name="points_for_my_something_happened_action" value="<?php echo get_option('points_for_my_something_happened_action'); ?>" size="2" /></td> <td><input type="button" onclick="document.getElementById('points_for_my_something_happened_action').value='0'" value="<?php _e('Do not add points','cp_buddypress'); ?>" class="button" /></td> </tr> <?php } add_action("bp_cubepoint_main_settings","example_show_cp_settings"); ?>
Please let me know if you added support for Cubepoints using this method & I’ll update this list:
Via buddydev.com

August 15th, 2011 on 5:48 pm
Hi Tosh,
Thanks for including it in the update. will certainly be helpful to many plugin developers including me
September 26th, 2011 on 5:18 pm
Hi Tosh,
Thanks a lot for this information. Really helped me a lot by saving time into understanding the code for CP.
I’ve integrated BP Like using this method but it was necessary to add the “do_action” as the plugin doesn’t run one when a “Like” action takes place.
September 26th, 2011 on 5:23 pm
That’s great! Can you share you code so others can benefit?
September 27th, 2011 on 1:43 am
Sure thing. I’ll organize it a bit since my BP Like already had some custom modifications. Will share it as soon as possible. Btw, wanted to mention i had trouble with this code:
if(!function_exists(“cp_points”))
return; // prevent any crash
Didn’t look much into it but that wouldn’t work properly. Always avoided the rest of the code to be used. Without it, works like a charm.
September 27th, 2011 on 3:45 am
Try using this instead and see if you get a error.
if(!function_exists(“cp_ready”))
September 27th, 2011 on 2:15 pm
Same problem. Will take a better look at it when i get some free time.
September 27th, 2011 on 2:29 pm
Sounds good. Try this when you have a chance:
if(!function_exists("cp_ready")) {return; // prevent any crash
}
September 27th, 2011 on 3:34 pm
I believe that’s the same code from the comment before
September 27th, 2011 on 3:36 pm
I added the { brackets to it. But ya that’s not needed. Just make sure you remove that code if you stop using CubePoints. But don’t know why you would want to do that
November 12th, 2011 on 6:48 pm
anyone successful with this for bbpress sitewide forums?
November 12th, 2011 on 6:55 pm
That is on my to-do list for creating a module for CubePoints. Just been busy with other projects. But I hope to get that working in the coming weeks. Since it seems so many people want it.
November 25th, 2011 on 12:47 am
looking forward to seeing you getting this to work with bbpress.
November 25th, 2011 on 6:07 pm
Done!
http://blog.slyspyder.com/2011/11/25/cubepoints-bbpress-2-0-together-at-last/
February 20th, 2012 on 10:45 am
Are there any modules of CubePoints for group specific points?
February 21st, 2012 on 9:48 pm
Not specifically but I use this to display text only on a certain group – http://pastebin.com/R7iDMN4W
I’m sure we could modify that slightly for your needs. To find the ID for your group, find the group avatar, and there should be something like this:
class="avatar group-25-avatar"So 25 would be the group ID.
March 15th, 2012 on 6:11 am
Thanx for the suggestion.
What I want is that each user’ points are stored according to the activity he/she does in that particular group and no universal point allocation. My points should vary in each and every group.
I had messed around with cp_bp_integration plugin passing group id to the functions but it work out well. Any suggestions regarding which specific files i should work on?
September 16th, 2012 on 7:08 pm
Hello Tosh,
Great work you been doing, i was wondering if you could help me out i wanted to display each users rank when they unlock and new rank it will be shown in the activity streams in buddypress
for example on the activity
user1 has unlocked level 2
user4 has unlocked level 4
here’s an image example of what i mean
http://i.stack.imgur.com/ml3fD.jpg
November 29th, 2012 on 5:52 pm
Thank you for the guide SlySpyder! It was very helpful in letting us tie into Cubepoints. I just wanted to let you know one way to utilize the $data attribute that we found helpful. I wanted to be able add points throughout our site without having to add a new line in our functions.php to log the message for each type of points awarded, thus allowing us to add points dynamically and adding a lot of flexibility. I looked at the cp-hooks.php file in Cubepoints and was able to see that they use the $data parameter to pass the message that shows up in the point log. For example, when you add a new comment to a blog post, a link to that post appears in the log. Mirroring this functionality, I was able to write a wrapper function around cp_points that passes in a custom message through the $data paramater(as well as a custom type and point value). This lets us use one function in functions.php to output the message stored in the $data parameter as the log message in the points log. I’ve posted my code below in case others might find it useful. This works for me, but no guarantee it will work for others, use at your own risk.
Put this in your functions.php
`if ( ! function_exists( ‘custom_cp_add_points’) ) :
/**
* Custom function to add cubepoints points programmatically and add custom points message to log
*
* Usage: custom_cp_add_points ($type, $user_id, $message, $points, $override);
*
* Paramaters:
* $type = (string)(required) unique string for point value identification
* $user_id = (int)(required) user id of user to receive points
* $message = (string)(optional) message to be displayed in user point list
* $points = (int)(optional) custom point value, will be overriden if an entry for this type exists in db
* $override = (bool)(optional) force override db point total with custom $points
*
*/
function custom_cp_add_points( $type, $user_id, $message = ‘Custom point addition’, $points = null, $override = false ) {
global $bp;
// Check if points value is already stored in database
$point_value = get_option($type);
// If point value in db and override false or points in db and no value passed in, use db
if ( ($point_value && $override == false) || ($point_value && $points == null ) ) {
$points = $point_value;
}
// If no db value and no points passed in, use default
elseif (!$point_value && $points == null) {
$points = 2;
}
// Add cubepoints
cp_points($type, $user_id, $points, $message);
}
endif; //custom_cp_add_points
function custom_cp_add_message($type, $uid, $points, $data) {
echo $data;
}
add_action(‘cp_logs_description’, ‘custom_cp_add_message’, 10, 4);
`
Then, anywhere in your site you can call something like:
`
custom_cp_add_points(‘custom_point_type’, bp_loggedin_user_id(), ‘My custom point message’, 10, true);
`
November 29th, 2012 on 6:04 pm
Just found a bug with my last post in that by echoing data in the second function you also display data that’s stored with other posts, looking for a work around.
November 29th, 2012 on 6:28 pm
Found a workaround, a bit of a hack though.
Add a line in the first function that sets $message = ‘some-unique-tag’ . $message;
Then in the custom_cp_add_message function add something like:
// See if message is from custom add message
if (strpos($data, ‘some-unique-tag’) === 0) {
// Remove ‘some-unique-tag’ from beginning of message
$data = substr($data, 15);
echo $data;
}
// Otherwise don’t output anything
else {
return;
}