Like many community websites, The Brew Place has its own user management system.  The user registration and management system is based entirely on its forums, which is an install of Simple Machine Forums v.1.1.5.  Also integrated into the website is an install of WordPress, which I like using to manage news articles and such.  Now since the three do not share the same way to manage logged in users (the base website uses both session variables and cookies, the forums and WordPress installs use just cookies, and obviously each cookie has different data) I had to write up a simple log-in crowbar for SMF allowing users to log-in on either the forums or the base website and they would be automatically logged-in to the other.  Now since the base website uses the forum’s members table to keep all of it’s membership data, that was not an issue.  However, with WordPress, it was an issue.

Now new members can actually only register through the forums – so if somebody clicks a register link on the base webpage, they’re simply forwarded to the forum’s register page.  Therefore, there needed to be something in there to make sure the entry is put into the WordPress users table as well.  I came up with an easy solution – since SMF already logs into the website, and vice-versa: why not make all of that into a WordPress plugin?  And so I did.  The wordpress plugin simply checks to see if there is either the forum cookie or the website’s session data, validates it, makes sure there is a corresponding user in its own users table, and then logs the user in.  Problem being I couldn’t find a really easy way to crowbar a user into being logged-in to WordPress, until I came up with the solution below:

[php]
function tbp_crowbar() {
/* I removed some code from here for security reasons */
if (isset($_SESSION['UserData'])) {
$SQL = Q(“SELECT `ID` FROM `news_users` WHERE `user_login`=? LIMIT 1″,$_SESSION['UserData']['Username']);
if (!$rs = mysql_query($SQL)) {
trigger_error(mysql_error(),E_USER_ERROR);
}
$user_id = mysql_result($rs,0,0);
$expiration = time() + 172800;
$username = $_SESSION['UserData']['Username'];
$cookie_data = $username . ‘|’ . $expiration . ‘|’;
$key = wp_hash($username . ‘|’ . $expiration, ‘logged_in’);
$cookie_data .= hash_hmac(‘md5′,$username . ‘|’ . $expiration,$key);
$auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
$logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, ‘logged_in’);
setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
setcookie(LOGGED_IN_COOKIE, $cookie_data, $expire, COOKIEPATH, COOKIE_DOMAIN);
} else {
setcookie(LOGGED_IN_COOKIE, null, time() – 60000, COOKIEPATH, COOKIE_DOMAIN);
wp_logout();
}
}
[/php]

The tbp_crowbar function is an action added to the “init” hook of WordPress by simply using the following line of code:

[php]
add_action(‘init’,'tbp_crowbar’);
[/php]

And voila!  If a user logs into the website and goes to the news, they’re logged in!  If they go to the forums and log in and then go to the news, they’re logged in!  If a new user registers (through the forums) and goes to the news, their user information is automatically ported to the WordPress users table, and they are also logged in!