Tag Archives: bbPress

How to Display the CubePoints Logs in bbPress User Profiles

I wanted to do create a page in the bbPress user profiles on which to display the CubePoints logs for the user, but I couldn’t find anything on Google about how to go about it. So I had to figure it out myself. Fortunately you don’t have to! It actually isn’t as hard as I though it might be.

Before we get started, let me say that this tutorial is based on bbPress 2.2.3, WordPress 3.5, and CubePoints 3.2.1 (the latest versions as of this writing). I have no idea whether this will work with older versions, but that’s no problem since you always install your updates (for security reasons if nothing else), right? RIGHT? Anyway, if anything changes in future versions I will try to remember to update this.

Also, you will need to install and activate my Front-end User Logs CubePoints module. You’ll find the code in this tutorial. Now lets get started!

Getting Started: Preparing a “Prettified” Link

Before we actually set up the page to display the points logs, we are going to prepare a “prettified” link for it. We can call the page “points”. The link for it will look something like "www.my-site.com/forums/users/someuser/points/" instead of "www.my-site.com/forums/users/someuser/?my_bbp_page=points" (though it might be a little different depending on your settings for the bbPress page slugs). This isn’t actually necessary, but it makes things… well, prettier. The code to do that looks like this:

<?php

/* Rewrite Rules */
function add_custom_rewrite_rules( $wp_rewrite ) 
{
	$new_rules = array(
		bbp_get_user_slug().'/([^/]+)/points/?$' => 'index.php?'.bbp_get_user_rewrite_id().'='.$wp_rewrite->preg_index(1).'&my_bbp_page=points',
	);

	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_custom_rewrite_rules');

/* Query Vars */
function add_custom_page_variables( $vars ) {

	$vars[] = 'my_bbp_page';
	
	return $vars;
}
add_filter( 'query_vars', 'add_custom_page_variables' );

?>

That can go in either a custom plugin, or in your child theme’s functions.php. The function add_custom_rewrite_rules() adds our custom rewrite rule to those already registered with WordPress. That is what enables the pretty links. The function add_custom_page_variables() adds 'my_bbp_page' to the allowed query variables. We must do this, or else WordPress will ignore the 'my_bbp_page' parameter when passed. And we need to be able to pass that parameter, since that is what we’re going to use later to display the points page. We can now call for it like this:

$wp_query->query_vars['my_bbp_page']

But in order for the rewrite to work, you will need to flush WordPress’s rewrite rules. The simplest way to do that is to visit the Settings->Permalinks page in your WordPress dashboard. (Whenever you visit that page, WordPress flushes the rules automatically).

Creating Your Own bbPress Templates

Now that we have prepared our prettified link, we can start to build the custom page in the bbPress user profiles. We will do this by creating our own bbPress templates. Actually, we will only be modifying the default bbPress templates (if you are already using custom bbPress templates, then you will need to modify them instead). The bbPress plugin has a very nice feature built into it: you can create your own templates to override the default ones. So you don’t have to modify any of the bbPress code. You just need to create a folder named “bbpress” in your child theme’s directory (if you aren’t using a child theme, then now is the time to start!). Any template that you put in there will override the default templates included with bbPress (in /wp-content/plugins/bbpress/templates/default/bbpress/). We are going to need to modify two templates; user-profile.php, and user-details.php. You can copy those to the “bbpress” folder you created in your theme directory. Now bbPress will use those files instead of its own.

The user-profile.php Template

Let’s start with the user-profile.php bbPress template. This is the template that bbPress uses when it displays the “Profile” page on the user’s profile. (The default page that shows the user’s biography.) What we are going to do is modify this template so that it can also display the user’s points logs. We’ll place all of the code within an if/else statement, that will display the user’s points logs if the 'my_bbp_page' query_var is set to “points”, or else it will display the user’s biography. The code looks like this (though if you have already made some modifications to the template, it will look different, of course):

<?php

/**
 * User Profile
 *
 * @package bbPress
 * @subpackage Theme
 */

?>

<?php

/* Custom Pages */

// If the my_bbp_page parameter is set...
if ( !empty($wp_query->query_vars['my_bbp_page']) ) {

	// Get the current page.
	$my_bbp_page = $wp_query->query_vars['my_bbp_page'];

	/* Points Page */
	if ($my_bbp_page == 'points'){
		// Styles.
		echo '<style>#bbp-user-body,#cp_logs_table_wrapper,#cp_logs_table,#points-table-clear{clear:right !important;}</style>';
		// Display the user's points logs.
		cp_modules_frontlogs_display();
	}
	
} else {

	/* Default Profile Page */
?>
	<?php do_action( 'bbp_template_before_user_profile' ); ?>

	<div id="bbp-user-profile" class="bbp-user-profile">
		<h2 class="entry-title"><?php _e( 'Profile', 'bbpress' ); ?></h2>
		<div class="bbp-user-section">

			<?php if ( bbp_get_displayed_user_field( 'description' ) ) : ?>

				<p class="bbp-user-description"><?php bbp_displayed_user_field( 'description' ); ?></p>

			<?php endif; ?>

			<p class="bbp-user-forum-role"><?php  printf( __( 'Forum Role: %s',      'bbpress' ), bbp_get_user_display_role()    ); ?></p>
			<p class="bbp-user-topic-count"><?php printf( __( 'Topics Started: %s',  'bbpress' ), bbp_get_user_topic_count_raw() ); ?></p>
			<p class="bbp-user-reply-count"><?php printf( __( 'Replies Created: %s', 'bbpress' ), bbp_get_user_reply_count_raw() ); ?></p>
		</div>
	</div><!-- #bbp-author-topics-started -->

	<?php do_action( 'bbp_template_after_user_profile' ); ?>
<?php } ?>

We check if the 'my_bbp_page' query variable is set. If it is and its value is “points”, then we display the points logs. The styles echo‘ed there may not be necessary for you, but I had to use them to get it to display right. (You can fiddle around with it an see what styles you need.) Then all we need to do is call the cp_modules_frontlogs_display() function (from the Front-end User Logs CubePoints module) and it displays the table with the user’s points logs in it.

If 'my_bbp_page' wasn’t set, then we just do the default stuff (the code below /* Default Profile Page */).

As you can see, all of this could be easily expanded if desired, and you could add more pages to the bbPress user profiles.

The user-details.php Template

Now that we have prepared the page to display the points logs, we need to add a link to it from the navigation menu. The code for that is in the user-details.php bbPress template. By default this displays a link to the user’s subscriptions, favorites, etc., on the left side below the user’s avatar. If you aren’t using the default user-details.php template, then of course the code will look different. But the basic concept is the same. We want to add a link to the /points/ page. And if your template does anything to highlight the various links to indicate the current page (like the default one does), you will need to do that to (if you aren’t sure what I mean, then see the code below). If you are using the default template, then the new code will look something like this:

<?php

/**
 * User Details
 *
 * @package bbPress
 * @subpackage Theme
 */

?>

	<?php do_action( 'bbp_template_before_user_details' ); ?>

	<div id="bbp-single-user-details">
		<div id="bbp-user-avatar">

			<span class='vcard'>
				<a class="url fn n" href="<?php bbp_user_profile_url(); ?>" title="<?php bbp_displayed_user_field( 'display_name' ); ?>" rel="me">
					<?php echo get_avatar( bbp_get_displayed_user_field( 'user_email' ), apply_filters( 'bbp_single_user_details_avatar_size', 150 ) ); ?>
				</a>
			</span>

		</div><!-- #author-avatar -->

		<div id="bbp-user-navigation">
			<ul>
				<li class="<?php if ( bbp_is_single_user_profile() && empty($wp_query->query_vars['my_bbp_page']) ) :?>current<?php endif; ?>">
					<span class="vcard bbp-user-profile-link">
						<a class="url fn n" href="<?php bbp_user_profile_url(); ?>" title="<?php printf( __( "%s's Profile", 'bbpress' ), esc_attr( bbp_get_displayed_user_field( 'display_name' ) ) ); ?>" rel="me"><?php _e( 'Profile', 'bbpress' ); ?></a>
					</span>
				</li>

				<li class="<?php if ( bbp_is_single_user_topics() ) :?>current<?php endif; ?>">
					<span class='bbp-user-topics-created-link'>
						<a href="<?php bbp_user_topics_created_url(); ?>" title="<?php printf( __( "%s's Topics Started", 'bbpress' ), esc_attr( bbp_get_displayed_user_field( 'display_name' ) ) ); ?>"><?php _e( 'Topics Started', 'bbpress' ); ?></a>
					</span>
				</li>

				<li class="<?php if ( bbp_is_single_user_replies() ) :?>current<?php endif; ?>">
					<span class='bbp-user-replies-created-link'>
						<a href="<?php bbp_user_replies_created_url(); ?>" title="<?php printf( __( "%s's Replies Created", 'bbpress' ), esc_attr( bbp_get_displayed_user_field( 'display_name' ) ) ); ?>"><?php _e( 'Replies Created', 'bbpress' ); ?></a>
					</span>
				</li>

				<?php if ( bbp_is_favorites_active() ) : ?>
					<li class="<?php if ( bbp_is_favorites() ) :?>current<?php endif; ?>">
						<span class="bbp-user-favorites-link">
							<a href="<?php bbp_favorites_permalink(); ?>" title="<?php printf( __( "%s's Favorites", 'bbpress' ), esc_attr( bbp_get_displayed_user_field( 'display_name' ) ) ); ?>"><?php _e( 'Favorites', 'bbpress' ); ?></a>							
						</span>
					</li>
				<?php endif; ?>

				<?php if ( bbp_is_user_home() || current_user_can( 'edit_users' ) ) : ?>

					<?php if ( bbp_is_subscriptions_active() ) : ?>
						<li class="<?php if ( bbp_is_subscriptions() ) :?>current<?php endif; ?>">
							<span class="bbp-user-subscriptions-link">
								<a href="<?php bbp_subscriptions_permalink(); ?>" title="<?php printf( __( "%s's Subscriptions", 'bbpress' ), esc_attr( bbp_get_displayed_user_field( 'display_name' ) ) ); ?>"><?php _e( 'Subscriptions', 'bbpress' ); ?></a>							
							</span>
						</li>
					<?php endif; ?>

					<li class="<?php if ( $wp_query->query_vars['my_bbp_page'] == "points") :?>current<?php endif; ?>">
						<span class="bbp-user-points-link">
							<a href="<?php bbp_user_profile_url(); ?>points/"><?php _e( 'Points', 'bbpress' ); ?></a>
						</span>
					</li>

					<li class="<?php if ( bbp_is_single_user_edit() ) :?>current<?php endif; ?>">
						<span class="bbp-user-edit-link">
							<a href="<?php bbp_user_profile_edit_url(); ?>" title="<?php printf( __( 'Edit Profile of User %s', 'bbpress' ), esc_attr( bbp_get_displayed_user_field( 'display_name' ) ) ); ?>"><?php _e( 'Edit', 'bbpress' ); ?></a>
						</span>
					</li>

				<?php endif; ?>

			</ul>
		</div><!-- #bbp-user-navigation -->
	</div><!-- #bbp-single-user-details -->

	<?php do_action( 'bbp_template_after_user_details' ); ?>

There are only two changes to the default code. The first is on line 27 where we change this:

				<li class="<?php if ( bbp_is_single_user_profile() ) :?>current<?php endif; ?>">

To this:

				<li class="<?php if ( bbp_is_single_user_profile() && empty($wp_query->query_vars['my_bbp_page']) ) :?>current<?php endif; ?>">

This is the code that adds the HTML class 'current' to the <li> element which contains the link for current page. That will make the link for the current page look different than the others, so you can easily see where you are. This particular piece of code does that for the ‘Profile’ page. This is the page that shows the user’s biography, the one whose template we modified so it could also display the points. All we do here is make sure that we are actually on the ‘Profile’ page, not displaying our custom points page. So we make sure that the 'my_bbp_page' query var is not set ( empty($wp_query->query_vars['my_bbp_page']) ). That way, the ‘Profile’ link won’t be highlighted as the current page when we are actually not displaying the profile, but the points.

The other change is on line 63, where we add the link to the points page:

					<li class="<?php if ( $wp_query->query_vars['my_bbp_page'] == "points") :?>current<?php endif; ?>">
						<span class="bbp-user-points-link">
							<a href="<?php bbp_user_profile_url(); ?>points/"><?php _e( 'Points', 'bbpress' ); ?></a>
						</span>
					</li>

We put that code within the if ( bbp_is_user_home() || current_user_can( 'edit_users' ) ) if statement, so that the link will only be shown to the user when they are logged in, and in their own profile area. Also, note the use of bbp_user_profile_url(), rather than hard-coding the link. This is so that our link will still work, even if the forum slugs change.

And that’s all there is to it. Note that I wrote all of this in a way that makes it easy to extend to create other custom pages for the bbPress user profiles, so you should be able to do that without much trouble. And since you seem to be interested in finding ways to integrate bbPress and CubePoints, maybe you should take a look at this CubePoints module, which allows you to award users points for creating posts in the bbPress forums.