Single Sign-on for a Wordpress Blog and BBPress Forum
I recently experimented with single sign-on for an existing Wordpress blog (2.7.1) and BBPress forum (0.9.4). The two sites were already visually integrated, but behind the scenes everything, including user logins, was separate. With some help from BobbyH (webmaster at Weddingbee.com) and a lot of digging into the BBPress forums, things worked out well and now users have a single login across both blog and forum.
“Reverse” integration is the situation where you have an existing Wordpress blog and BBPress forum that you would like to integrate. The (by far) simpler option, referred to as “normal” integration, is to set up them up as integrated from the start, or even with an existing blog and a brand new forum. There is a great description of these options on the BBPress forum in this thread, and this screencast.
There is very little description of the more complicated reverse integration scenario, so I thought I would lay out the steps here. Most of them (all but the final two) are database operations. I used phpMyAdmin to do these operations (your host probably provides this tool), but you could use whatever tool you have for MySQL admin. You could even do it directly from the command line.
My basic approach here is to use the Wordpress database to hold one set of master tables for the user information, and to point BBPress to these tables. I use the BBPress tables as the starting point for these master tables because the forum has thousands of users. I choose to keep only the user tables in the same database. The rest of the BBPress data (e.g. the posts themselves) are still kept in the separate BBPress database.
So the high level steps are:
- Copy the BBPress user tables into the Wordpress database
- Replace the Wordpress user tables with these new user tables
- Modify the new tables so that they are usable by Wordpress
- Map the old Wordpress users to users in the new tables
- Set things up to do cookie sharing correctly so people logged into the blog transition seamlessly into the forum and vice versa.
Here are the steps in more detail:
- I didn’t need to do backups because I was using a test site, but if you are trying this, don’t go any further without full backups of everything.
- Copied the “bb_users” and “bb_usermeta” tables from the BBPress database to the Wordpress database. I was a bit worried that the row sizes weren’t exactly the same in the destination copy, but as it turns out, that wasn’t an issue.
- Added a “user_activation_key” field to the copy of “bb_users” to ensure that the schema matched my current “wp-users” table in the Wordpress database.
- Renamed the “wp_users” and “wp_usermeta” tables to “wp_users_old” and “wp_usermeta_old” respectively.
- Renamed the new “bb_users” and “bb_usermeta” tables to “wp_users” and “wp_usermeta” respectively.
- Added Wordpress admin rights and metadata to the new admin user. To do this I ran the following query:
INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES ('1', 'wp_capabilities', 'a:1:{s:13:"administrator";b:1;}'); - Added Wordpress user metadata for the rest of the users (since I’m using the
BBPress user tables as the starting point they know nothing about WP capabilities). Here is the query:INSERT INTO wp_usermeta (user_id, meta_key, meta_value) SELECT user_id, 'wp_capabilities' AS meta_key, 'a:1:{s:10:"subscriber";b:1;}' AS meta_value from wp_usermeta WHERE user_id NOT IN (SELECT user_id from wp_usermeta WHERE meta_key = 'wp_capabilities') GROUP BY user_id; - Changed the user_id references in the “wp_posts” and “wp_comments” tables so that they pointed to the correct users (you need to do this because equivalent users don’t necessarily have the same ID in the previously separate Wordpress and BBPress databases). Here are the queries I ran:
UPDATE wp_posts SET post_author=NEW_ID WHERE post_author=OLD_IDNOTE that you need to think carefully about the order in which you do these changes. You don’t want to change a user’s ID to one that already exists in the Wordpress table.
UPDATE wp_comments SET user_id=NEW_ID WHERE user_id=OLD_ID
- Installed superann’s plugin to downgrade WP 2.7.1’s cookie handling for compatibility with BBP 0.9.4
- Told BBPress to find its user data in the new Wordpress user tables. This is done in the BBPress admin page for Wordpress integration: [...]/bb-admin/options-wordpress.php. You need to follow the instructions carefully. In particular, note that the Wordpress database secret should be copied from the current Wordpress setting and not vice versa.
This is not something to do in a rush. I checked the results of each step carefully before moving on. This included browsing the database and, later in the process, logging in to the blog and forum to check whether things were working as expected.
And the job is not complete. I can now log in users on either blog or forum and they are automatically logged in when they transition to the other. If they log off on either side, they are logged of from both. What I still need to do is create a login panel that is visible from both blog and forum. Right now, users can only log in on the forum side.
NOTE:
- This integration approach worked for me, but your mileage may vary. There are many permutations for the configuration of Wordpress and BBPress and I’ve made no attempt to present a completely general solution with the steps above.
- Operating directly on your database is not for the faint of heart, so only tackle this if you are confident with phpMyAdmin, or if you are experimenting on a test site.
Posted: May 10th, 2009 under Development.
Tags: bbPress, Database, Expert, Wordpress
