Search

Creating a Voting Plugin for bbPress Forums

Listen to this article

bbPress Post LikeThe bbPress forum plugin, is a really simple and easy to setup forum plugin for WordPress. It integrates seamlessly into WordPress, and certainly so, because it has been created by the makers of WordPress themselves. Because the intention is to provide a light-weight and speedy forum software, it does not have several features, which other forum plugins might have. But you can certainly extend the features bbPress provides you, using a custom extension.

 

Creating a Voting Extension Plugin for bbPress

Usually on forums, (unless may be it is a support forum), there is an option to rate topics, or posts. The number of votes provides an immediate indication of the popularity/importance/usefulness of a post, to a visitor.

 

Add Images as Voting Options

Instead of providing a button to vote, you can make use of images, using simple CSS. The images will act as a button, and record every click in a counter. The count will also be displayed alongside the image. (You can of course use buttons, but images such as a ‘thumbs up’ or ‘thumbs down’ image, ‘up arrow’ and ‘down arrow’, etc., can be used to provide an intuitive interface. If you do not want to use images, you can use ‘+’ and ‘-’, text). If you want to allow only a ‘Like’ option, only a single image needs to be provided, with a counter.

 

Adding the Voting Options to Forums

For bbPress, voting options can be added at the topic level or at each post level. In a website, such as stackoverflow, you will notice that there is a voting counter for each question, along with voting options for each reply.

 

Voting Options (Like/Dislike buttons), can be placed anywhere needed. The important part here, is to find a suitable hook, which can allow you to place the voting options.

  • For each Post: An option to vote for each post can be provided alongside post admin links. Admin links, are indicated by links available after the post date, and are part of the post meta. For this, we can use the hook:

bbp_theme_after_reply_admin_links

  • For each Topic: To add a voting option only for each topic, you can use the below hook, to add your voting interface:

bbp_template_before_single_topic

But this will add an option only on a single topic page. To indicate vote counts on topic index, you will need additional customization.

  • For each User: Forums do not usually allow users to vote for other users. Users can be rated, based on the number of votes their posts receive. For example, if a user has 5 posts, each with 4 (Like/Up) votes, he will receive a rating of 20. This rating, can be indicated alongside the post author information, using the filter:

bbp_get_reply_author_link

You can even create an entire user rating system, based on ratings, by assigning points if a topic is posted, additional points for replying to topics posted by others, or in other forums, etc.

 

How Voting Data is Saved

Voting Data should be saved for not only for each post (or topic), but for each user as well. This is to restrict a user from voting twice. This information should be saved as metadata for a post and a user. For example, when a user ‘usr_abc’, votes on a post ‘postid_xyz’, vote count for ‘postid_xyz’ should be increased using ‘update_post_meta’. And user meta should be updated, using ‘update_user_meta’, to indicate that the ‘usr_abc’ has voted on ‘postid_xyz’.

Since users will vote on several posts, the meta field should contain an array of post ids, a user has voted on. If there is an option, say an ‘Up’ vote or a ‘Down’ vote, then that option value selected can be saved as well, indicating the person’s preference. For example, ‘usr_abc’s’ meta field value for ‘bbpress_forum_ratings’, would look something like this,

//meta field key: bbpress_forum_ratings
//meta field value: array (
array(‘postid_xyz’, 1),
array(‘postid_hjk’, -1),
array(‘postid_lmn’, 1),…..
)
// where 1 = ‘Like/Up’ and -1 = ‘Dislike/Down’

 

Putting it All Together

Now that we have the concepts in place, we can create a simple voting plugin for bbPress forum. That adds a ‘Like’ option, for every post. You can use the above hooks mentioned, to add a similar option, or to extend the plugin, the way you prefer. The ‘Like’ option can be decorated using simple CSS.

[pre]//adding the ‘Like’ option
add_action(“bbp_theme_after_reply_admin_links”, “wdm_add_bbpress_post_like_option”);
function wdm_add_bbpress_post_like_option(){
$post = bbpress()->reply_query->post;
if($post != NULL){
$postID = $post->ID;
$postVotes = (int)get_post_meta($postID, “bbpress_post_votes_count”, true);
// display current vote count for post
echo ‘<div>’. $postRating. ‘Votes</div>’;
//check if current user can vote
// can_user_vote_on_post (add conditions such as has user already voted, permissions, etc.)
if(can_user_vote_on_post($postID)){
// if user can vote, add a ‘Like’ link
echo ‘<a href=”#” onclick=”bbpress_post_vote_link_clicked(‘ . $postID . ‘); return false;”></a>’;
}
}
}

// handle ‘Like’ clicked
add_action(“wp_ajax_bbpress_post_vote_link_clicked”,”bbpress_post_add_vote”);
function bbpress_post_add_vote(){
if(is_user_logged_in()){
$postID = $_POST[“postID”];
$userID = bbpress()->current_user->id;
$userRatings = get_user_meta($userID, “bbpress_post_votes”, true);
// if user has not voted, add user vote
if(!isset($userRatings[$postID])){
$postVotes = (int)get_post_meta($postID, “bbpress_post_votes_count”, true);
$postVotes = $postVotes+1;
update_post_meta($postID, “bbpress_post_ratings_rating”, $postVotes);
$userRatings[$postID] = true;
update_user_meta($userID, “bbpress_post_ratings_ratings”, $userRatings);
}
}
}[/pre]

 

Here is your jQuery AJAX function, when ‘Like’ link is clicked. (Reference: How to use wp_ajax)

[pre]function bbpress_post_vote_link_clicked(postID){
jQuery.post(ajaxurl, {
action: “bbpress_post_vote_link_clicked”,
postID: postID
}, function(response){
// disable or remove ‘Like’ option, once the user has voted.
});
}[/pre]

Remember to enqueue, your custom stylesheet and scripts, using the ‘wp_enqueue_scripts’ hook.

 

The reason why many people choose not to work with bbPress, is because of the lack of documentation available. If there is any use case you want help with, be sure to write to us in the comment section below. We might just write a tutorial for you.

Akshaya Rane

Akshaya Rane

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads