Karma Points System for bbPress

This is a simple karma points and badges system for bbPress. It’s just a few PHP functions.

There are 4 badges, and they increase in rank from lowest to greatest: Member, Mentor, Guiding Light, Sage. When a users registers an account in your bbPress forum, they will automatically get the “Member” badge and they will earn their first karma point.

As they earn more karma points, their badge will level up in rank to the next badge.

The badge also matters because the higher their rank, the more karma points they earn per action.

How Users Can Level-up Their Badge

  • Everyone who registers will automatically get the “Member” badge.
  • A user must earn 60 karma points to move up to the “Mentor” badge.
  • A user must earn 150 karma points to move up to the “Guiding Light” badge.
  • A user must earn 300 karma points to move up to the “Sage” badge.

These number thresholds can be modified on lines 24, 25, and 26 of the code below.

How To Change the Badge Names

You can change the 4 badge names in the code below to your desired badge names. Remember, the hierarchy is from lowest to greatest rank, so:

  • Replace Member in the code below with your own name for your lowest ranking badge.
  • Replace Mentor with your own name for your slightly higher ranking badge.
  • Replace Guiding Light with your own name for your next highest ranking badge.
  • Replace Sage with your own name for your top highest ranking badge.

Each badge name appears twice in the code below so be sure to change it in both places. Also note that these are names that can be displayed on the front end, so uppercase letters and spaces are okay.

How Many Karma Points Earned

Users or Topic Creators that have the “Member” badge will get this many karma points in these cases:

  • User gets 2 karma points when starting a new topic.
  • When a user subscribes to a topic, the topic creator gets 2 karma points.
  • When a user favorites a topic, the topic creator gets 5 karma.
  • For each reply: topic creator gets 4 karma, and the current user making the reply gets 4 karma, unless user is replying to their own topic, in which case, user gets only 1 karma (none for being the topic creator).

Users or Topic Creators that have the “Mentor” badge will get double the points of a Member.

Users or Topic Creators that have the “Guiding Light” badge will get three times the points of a Member.

Users or Topic Creators that have the “Sage” badge will get four times the points of a Member.

Jump down to see a sample of the text you can add to your forum FAQ to let users know how they can earn karma. Note that you should edit that text if you modify the code to change the points amounts.

The Code

PHP

<?php

/**
* Karma points system for bbPress
* @param int $amount Number of new karma points to add
*/
function isa_add_karma($amount, $user_id) {

	// multiply amount by rank

	$old_badge = get_user_meta($user_id, 'badge', true);

	if('Mentor' == $old_badge) $amount *= 2;
	elseif ('Guiding Light' == $old_badge) $amount *= 3;
	elseif ('Sage' == $old_badge) $amount *= 4;

	$points = $amount + (int) get_user_meta($user_id, 'karma', true);
	update_user_meta($user_id, 'karma', $points);

	// update badge level if needed

	$badge = '';

	if($points >= 300) $badge = 'Sage';
	elseif ($points >= 150) $badge = 'Guiding Light';
	elseif ($points >= 60) $badge = 'Mentor';

	if($badge && ($badge != $old_badge)) update_user_meta($user_id, 'badge', $badge);

}

// Earn 2 karma every time you start a new discussion.
add_action('bbp_new_topic', function($topic_id, $forum_id, $data, $topic_author){
	isa_add_karma(2, $topic_author);
}, 99, 4);


// Every user earns 1 karma and 'Member' badge upon registration
function isa_insert_new_user_karma($meta, $user, $update){
	if(empty($update)) {
		$meta['karma'] = 1;
		$meta['badge'] = 'Member';
	}
	return $meta;
}
add_filter('insert_user_meta', 'isa_insert_new_user_karma', 10, 3);


// When a user subscribes to a topic, topic creator gets 2 karma
add_action('bbp_add_user_subscription', function($user_id, $object_id) {
	if( bbp_is_topic($object_id)) {
		$topic_creator_id = get_post_field( 'post_author', $object_id );
		// original topic creator gets 2 points, but not when you subscribe to your own created topic
		if($user_id != $topic_creator_id) isa_add_karma(2, $topic_creator_id);
	}
}, 10, 2);
// When a user favorites a topic, topic creator gets 5 karma
add_action('bbp_add_user_favorite', function($user_id, $topic_id){

	$topic_creator_id = get_post_field( 'post_author', $topic_id );
	isa_add_karma(5, $topic_creator_id);

}, 10, 2);

/**
 * On each reply: topic creator gets 4 karma, current replier gets 4 karma,
 * unless user is replying to their own topic, in which case, user gets only 1 karma.
 */
add_action('bbp_new_reply', function($reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author, $is_edit) {

	if(! $is_edit) {
		// if replier is not topic creator, both get 4, otherwise user gets 1 for replying to own topic
		$topic_creator = get_post_field( 'post_author', $topic_id );
		if($reply_author == $topic_creator) {
			isa_add_karma(1, $reply_author);
		} else {
			isa_add_karma(4, $topic_creator);
			isa_add_karma(4, $reply_author);
		}
	}
}, 10, 6);

// show the badge name instead of regular Roles
add_filter('bbp_get_user_display_role', function($role, $user_id) {
	return get_user_meta( $user_id, 'badge', true );
}, 10, 2);

How To Display The Badge Name

By default, the badge name will be displayed instead of the regular forum Roles. In addition, you can display the badge name anywhere, like for example:

If your theme has a custom bbPress template folder, find the file named “content-single-user.php” if it exists. If not, you can copy it from the bbPress plugin at this location:

wp-content/plugins/bbpress/templates/default/bbpress/content-single-user.php

…Copy that file to your own bbpress folder in your own theme.

In that file, you can use this to display the badge name for the user:

<?php echo get_user_meta( bbp_get_user_id(), 'badge', true ); ?>

How To Display Karma Points

To show the karma points next to the badge only where badge has replaced the Role, add this to the functions above:

// Show the Karma points next to badge only where badge has replaced the Role

add_filter('bbp_get_user_display_role', function($role, $user_id) {
	$out = get_user_meta( $user_id, 'badge', true );
	$out .= " &nbsp; (" . (int) get_user_meta( $user_id, 'karma', true ) . ' karma)';
	return $out;
}, 50, 2);

To show the karma points somewhere else on the user profile:

If your theme has a custom bbPress template folder, find the file named “user-profile.php” if it exists. If not, you can copy it from the bbPress plugin at this location:

wp-content/plugins/bbpress/templates/default/bbpress/user-profile.php

…Copy that file to your own bbpress folder in your own theme.

In that file, you can use this to display karma points for the user:

<?php echo (int) get_user_meta( bbp_get_user_id(), 'karma', true ); ?>

(The (int) is there as security instead of escaping.)

You can dress it up with a Yin-Yang character like this:

<p class="karma">&#9775; &nbsp; <?php echo (int) get_user_meta( bbp_get_user_id(), 'karma', true ); ?> karma</p>

How do user’s earn karma?

If you’re a “Member”:

  • Earn 1 karma when you join the site (register).
  • Earn 2 karma every time you start a new discussion.
  • Earn 2 karma every time someone else subscribes to a discussion that you started.
  • Earn 1 karma every time you reply to a discussion that you started.
  • Earn 4 karma every time you reply to a discussion that someone else started.
  • Earn 4 karma every time someone **else** replies to a discussion that you started.
  • Earn 5 karma every time someone favorites a discussion that you started.

If you’re a Mentor, you earn double the points of a regular Member. That is:

  • Earn 4 karma every time you start a new discussion.
  • Earn 4 karma every time someone else subscribes to a discussion that you started.
  • Earn 2 karma every time you reply to a discussion that you started.
  • Earn 8 karma every time you reply to a discussion that someone else started.
  • Earn 8 karma every time someone **else** replies to a discussion that you started.
  • Earn 10 karma every time someone favorites a discussion that you started.

If you’re a Guiding Light, you earn 3 times the points of a regular Member. That is:

  • Earn 6 karma every time you start a new discussion.
  • Earn 6 karma every time someone else subscribes to a discussion that you started.
  • Earn 3 karma every time you reply to a discussion that you started.
  • Earn 12 karma every time you reply to a discussion that someone else started.
  • Earn 12 karma every time someone **else** replies to a discussion that you started.
  • Earn 15 karma every time someone favorites a discussion that you started.

If you’re a Sage, you earn 4 times the points of a regular Member. That is:

  • Earn 8 karma every time you start a new discussion.
  • Earn 8 karma every time someone else subscribes to a discussion that you started.
  • Earn 4 karma every time you reply to a discussion that you started.
  • Earn 16 karma every time you reply to a discussion that someone else started.
  • Earn 16 karma every time someone **else** replies to a discussion that you started.
  • Earn 20 karma every time someone favorites a discussion that you started.

See more:

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]