The Professional Membership Plugin for WordPress

 

Integrating Your Members with BBPress

WARNING – This guide was written for Your Members v1.9.x.x or lower, In Your Members 1.10.0 some menu items have changed, we will be updating this documentation as soon as we can.

In our previous tutorial we showed you how you could access Your Members functions and using them in your own WordPress plugins. In this tutorial we are going to look at how we can access YM functions beyond WordPress itself. The final tutorial in this series will deal with Your Members in separate complete third party scripts that do not share a common WordPress user base.

BBPress – WordPress for Forums

Still in its infancy BBPress is a forum software designed by the people who make WordPress and it on a good day integrates nicely. One of the key features is that it can use WordPress own user tables making it the perfect forum software to integrate Your Members with.

Installing BBPress

In this tutorial I used WordPress 2.6 and BBpress Alpha 1 (alpha2 still has issues) you can use BBPress 0.9 (the latest stable branch) and WordPress 2.5 but you can’t easily use WordPress 2.6 and 0.9 togeather due to a number of security changes to WordPress. Likewise you cannot use WordPress 2.5 and BBpress 1 Alpha.

Once you have selected your version, install WordPress (I assume you have already done that) followed by BBPress the install is pretty painless. The important thing is to use WordPress User Tables and not those of BBPress.

If you are having problems the following should help.

For this tutorial we do not require a “complex” integration as long as WordPress and BBPress are sharing tables that should be sufficient.

Setting up the Forums

Ok so now we have a ‘Forum’ lets set up some Forums, go into the manage section and then forums and create the forums as you would like them including ones we are going to hide later.
Choose the forums you are going to hide and what YM user account types should have access to which forum, I suggest you jot it down on a piece of paper.

Your Members Integration

Because our BBPress is using WordPress user tables our lives are much easier, however BBPress is not WordPress so we are going to have to grab the features we want ourselves rather then relying on Your Members to provide the functions.

BBPress to Your Members user account types


function bb_ym_get_user_account_type() {
  global $wp_users_object, $bb_current_user;
  $account_type = 'Guest';
  if ($bb_current_user) {
  $user = $bb_current_user->ID;

 foreach ($bb_current_user->ym_user as $key=>$value) {
  if ($key == 'account_type') {
  $account_type = strtolower($value);
  } 
  }
  }
  return $account_type;
  }


Ok so basically we calling the current users which is handled a little differently then in WordPress, then calling ym_users which is stored in the wp_meta_user table hence why we can pull it so easily. Get out of that the account type associated with it, by including a default guest type we have something to fall back on should the user not have an associated Your Members Account Type.

Hidden Forums Plugin and Your Members

BBPress doesn’t actually have the ability to show forums to only certain user roles, so we need to use a plugin to add this ability. There are several but they all seem to have their drawbacks the one I have chosen is called Hidden Forums by an active BBPress developer _ck_ His was the tidiest and easy to adapt the downside is that it has no pretty interface so we will have to add our forums into the code itself.


$ym_hidden_forums['ym_hidden_forums']=array(2,3,5);	// hide these forums, list by comma seperated number

$ym_hidden_forums['ym_allow_roles']['all_forums']=array('keymaster');		// these roles can always see ALL forums regardless
  $ym_hidden_forums['ym_allow_roles'][2]=array('member');	// exact Your Member Account type (in lowercase)

$ym_hidden_forums['ym_allow_roles'][3]=array('member','free');	// exact Your Member Account type (in lowercase)
$ym_hidden_forums['ym_allow_roles'][5]=array('member','free');	// exact Your Member Account type (in lowercase)

$ym_hidden_forums['allow_users']['all_forums']=array(1);		// these users can always see ALL forums regardless


So our first line is the forums we wish to hide in our case we want to hide forums 2,3 and 5 the next lines are for each of those forums and who can access them, notice all the account types are in lower case, because in our get_account_type function we converted its result to lowercase to make life easier for everyone.

Next we make a tiny change to the private forum code on line 74


 $role=@reset($bb_current_user->roles);


To:


 $role= bb_ym_get_user_account_type(); 

That’s it job done, our script is now showing and hiding forums based on Your Members account type level.

Integrating registration & Login

One of the problems with our current setup is dealing with the fact both BBPress and WordPress can register people. We of course want only WordPress to register people s that is where we are processing payments and generating custom fields etc. Unfortunately BBPress doesn’t provide a simple way for us to redirect people to our WordPress page so we will have to resort to using .htaccess file to redirect.

In your .htaccess you will want to add…

Redirect 301 /PATHTOBBPRESS/register.php http://URLTOWORDPRESS/wp-login.php?action=register


where one is a relative path the other a url, of course you could redirect this to a sales page first if you want to explain the benefits of joining. Once this is in place users shouldn’t be able to accidentally register on the forum. You can do the same with bb-login.php however there is no real need to as only those who registered can login, however in a very small number of cases someone with very recently expired account maybe able to access the forum even though their subscription has run out, however the next time they login it will be blocked until they pay their dues.

The Code and sum up

I have included the complete code in the file below, I’m afraid its released under GPL 2 and not our normal Creative Commons license as that was the license the original author used. Hopefully you will be able to see the code in practice and easily add it to your own BBPress plugins or adapt others to suit your needs.

Download Your Members – Hidden Forum code

Any problems check out the discussions on the forum.