MyBB Community Forums

Full Version: MyBB - Wordpress user integration - alpha 0.1
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Yes just give me a few minutes i have to run
The external db reloaded plugin for wordpress also does this it may be worth checking that out as an option
seems complex might just use WordBB or code a Mybb compatible one from scratch.

gets stuck in redirect loop..
After testing and trying to get many plugins to work I finally got this to work Big Grin I will have no choice to use this on my A live site as nothing else works but from what I can see it is actually pretty stable Wink

Here's what to do to fix the redirect loops
Navigate to the plugin settings
now make a new folder in your forums root called "check.php"
and put the following code inside the folder:

<?php
echo getcwd(); 

?>

Now navigate to that file in your browser you will see something like C:/path-to-forum (this is what I see on my local server and I have not tested this on a live server but it should still work) Copy the information you see on the page in to the Absolute Plugin Path Setting, Now log out of your forum & wordpress sites, After this login to your forum again.

Done Wink

Hope this helps some users.

Now all of a sudden the dumb plugin is not working how do you figure that out :/ I just disable wordpress comments and if my users ask I just blame it on the compatibility between both softwares. Spent days trying to get this to work

I try my best....

All of a sudden it is working again I will need to have a look tomorrow.

(2013-11-15, 02:41 PM)Vortex00 Wrote: [ -> ]First off, thanks for taking the time to write this.

my settings are:
Mybb 1.6.10
WordPress 3.7.1

I cannot get this to work.
Tired several setting both in the settings for the plugin in wordpress and also in MyBB. I also used the recommended settings in the picture.

I also looked at the cookie names that were being generated by MyBB, both in firefox and chrome, they both matched what I had set the cookie name to in the plugin settings. The other options seem pretty straight forward.

after I save the settings with the plugin, it pretty much kicks me out of the wp-admin and you can no longer log back in because the plugin is not working. Only way to get back in was to delete/change the plugin file name.

I have got this to fully work now I have resolve all problems Wink

I will make a video on you tube explaining how to do it on my channel tomorrow so be ready Wink


I will try my best in the video to save you all the trouble but this plugin does work well just some minor issues which I also show you how to fix in the video.
Alright after a few days looking and modifying the code, I have cleared out the redirect bugs, Just make sure you have a index home page named "index.php" as I have added ../index.php one directory down for a homepage as I have a custom sign in page for Mybb.

<?php
/**
 * Plugin Name: MyBB-WP User Integration
 * Plugin URI: http://www.wordpress.com
 * Description: Authenticate and create users using mybb cookies. You can login and logout both from mybb and wordpress. Read README.txt file carefully for setup instructions.
 * Version: alpha 0.1
 * Author: Alder
 * Author URI: http://URI_Of_The_Plugin_Author
 * License: GPL2
 */

 /* Plugin initialization */

 
 function wp_mybb_cookie_activation () {
        global $wp_roles;

        $wp_roles->remove_cap( 'administrator', 'add_users' );
        $wp_roles->remove_cap( 'administrator', 'create_users' );

	if ( ! current_user_can( 'activate_plugins' ) )
	   return;

	add_option('mybb_wp_user_table','mybb_users');
	add_option('mybb_wp_cookiename', get_cookie_name ());
	add_option('mybb_wp_defaultrole',get_option('default_role'));
	add_option('mybb_wp_forumpath','/forum');
 }

 function wp_mybb_cookie_deactivation () {
       if ( ! current_user_can( 'activate_plugins' ) )
	 return;
       global $wp_roles;
       $wp_roles->add_cap( 'administrator', 'add_users' );
       $wp_roles->add_cap( 'administrator', 'create_users' );
       $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
       check_admin_referer( "deactivate-plugin_{$plugin}" );
 }
 
 function wp_mybb_cookie_uninstall () {
	if ( ! current_user_can( 'activate_plugins' ) )
	    return;
        check_admin_referer( 'bulk-plugins' );

        delete_option('mybb_wp_user_table');
	delete_option('mybb_wp_cookiename');
	delete_option('mybb_wp_defaultrole');
	delete_option('mybb_wp_forumpath');
 }
 
 function wp_mybb_cookie_init (){
	register_setting('mybb_wp','mybb_wp_user_table');
	register_setting('mybb_wp','mybb_wp_cookiename');
	register_setting('mybb_wp','mybb_wp_defaultrole');
	register_setting('mybb_wp','mybb_wp_forumpath');
 }
 
 
 /* Admin Menu */
 
 //page for config menu
function wp_mybb_cookie_admin_menu () {
	add_options_page("MyBB-WP User Integration", "MyBB-WP Integration", 10, __FILE__ ,"wp_mybb_cookie_admin_options");
}

//admin configuration 
 function wp_mybb_cookie_admin_options () {
	?>
	 <div class="wrap">
		<div id="icon-generic" class="icon32"><br /></div>
        <h2>MyBB-WP User Integration</h2>
        <form method="post" action="options.php">
            <?php settings_fields('mybb_wp'); ?>
			<table class="form-table">
			
				<tr valign="top"><th scope="row">MyBB users table name</th>
					<td><input class="regular-text"  type="text" name="mybb_wp_user_table" value="<?php echo get_option('mybb_wp_user_table'); ?>" /></td>
					<td><span class="description">You should check this value in your database. Usually the right value is mybb_users</span></td>
				</tr>
				
				<tr valign="top"><th scope="row">Absolute path to forum's folder</th>
					<td><input class="regular-text"  type="text" name="mybb_wp_forumpath" value="<?php echo get_option('mybb_wp_forumpath'); ?>" /></td>
					<td><span class="description">The absolute path in which your forum is installed.<br /> If your website is www.example.com and your forum is in www.example.com/forum the right value will be /forum (no trailing slash)</span></td>
				</tr>
				
				<tr valign="top"><th scope="row">New Users Default Role</th>
					<td><select name="mybb_wp_defaultrole" id="mybb_wp_defaultrole"><?php wp_dropdown_roles( get_option('mybb_wp_defaultrole') ); ?></select></td>
					<td><span class="description">When an user moves from the forum to wordpress for the first time, a new user will be created using his cookie. This is the default role he will be assigned to.</span></td>
				</tr>
				
				<tr valign="top"><th scope="row">MyBB user cookie name</th>
					<td><input class="regular-text"  type="text" name="mybb_wp_cookiename" value="<?php echo get_option('mybb_wp_cookiename'); ?>" /></td>
					<td><span class="description">Autofilled value is usally correct (this value SHOULD be correct accordingly to YOUR database: <strong><?=get_cookie_name();  ?></strong> ).<br /> To test if it is correct you can logout both from wordpress and MyBB and then login in MyBB. If you are not automatically recognized from wordpress, you must check this value looking at your cookies.</span></td>
				</tr>
				
				<tr valign="top"><th scope="row">Remember to disable registration</th>
					<td><? 
							if (get_option('users_can_register') ) 
								echo "<span style=\"color:red;\">User registration enabled, disable it from <a href=\"options-general.php\">General Settings</a></span>" ;
							else
								echo "<span style=\"color:green;\">User registration disabled, YEAH!</span>";
						?></td>
					<td><span class="description">Users registration *SHOULD* be disabled in WP and enabled only in MyBB. Edit your theme to change registration url.</td>
				</tr>
				
			
			</table>
            <p class="submit">
            <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
            </p>
        </form>
    </div>
	<?php
 }
 
 /**
 * get_cookie_name()
 *
 * Retrieve the mybb user cookie name using values from configuration and mybb databse
 * return a string containing the name of the cookie generated by mybb
  *
 */
 function get_cookie_name () {
	global $wpdb;
	$mybb_table_prefix =  str_replace( 'users', '', get_option('mybb_wp_user_table') );//here I assume that mybb's user table is "users" more a prefix
	$mybb_cookieprefix = $wpdb->get_var("SELECT value FROM " . $mybb_table_prefix . "settings WHERE name = 'cookieprefix' ");
	return $mybb_cookieprefix. 'mybbuser';
 }
 
 /**
 * get_mybb_cookie()
 *
 * returns an array with info stored in mybb cookie
 * $cookie[0] = user id 
 * $cookie[1] = loginkey
 *
 */
 function get_mybb_cookie () {
 	//Read mybb cookie user details
	if ( ! empty ($_COOKIE[get_option('mybb_wp_cookiename')] ) ) {
		$cookie = explode( '_', $_COOKIE[get_option('mybb_wp_cookiename')]);
		return $cookie;
	} else {
		return FALSE;
	}
 }
 
 /**
 * mybb_user_login_cookie()
 *
 * sets login infos using mybb cookie
 * If user doesn't exists I create a new one but with a fake password
 *
 */
 function mybb_user_login_cookie (){
	if( is_user_logged_in() ){ //is user already logged in?
		//let's check if mybb cookie exists. if not, delete also wp one
		// the only case in which mybb cookie wouldn't exists, is because the user logged out from mybb
		if ( ! get_mybb_cookie()) {
			//echo "no mybb cookie<br>";
			wp_logout();
			exit();
		}
	} else { // user isn't logged in, let's go
		//use mybb's cookie to auth in wordpress
		if ($cookie = get_mybb_cookie() ) {
		
			global $wpdb;
			//if we have cookie, then try to get user data and verify that the user can login
			if ($cookie[0]) {
				//I get user data from mybb database
				$mybb_user_data = $wpdb->get_row("SELECT username, password, loginkey, email, website FROM " . get_option('mybb_wp_user_table') . " WHERE uid = '$cookie[0]'");

				if ( $mybb_user_data->loginkey == $cookie[1] ) {
					
					//I check if user exists in wordpress database using mybb id
					$wp_user_id = $wpdb->get_var( 'SELECT ID FROM ' . $wpdb->users . ' WHERE user_email = "' . $mybb_user_data->email . '" AND user_login = "' . $mybb_user_data->username . '"' );
					if ( $wp_user_id > 0) { //user already present in wp database
						$user_info = get_userdata($wp_user_id);
						wp_set_auth_cookie($wp_user_id, true, false);

						do_action('wp_login', $user_info->user_login);
						
						if (wp_get_referer()) {
							header("Location: ../index.php");

						} else {
							
						}
						exit();
					} else { //user not present in wp database, let's create it
						$new_wp_userdata['user_login'] = $mybb_user_data->username;
						$new_wp_userdata['user_pass'] = $mybb_user_data->password; // the user will never input this password, so its value isn't really meaningful
						$new_wp_userdata['user_url'] = $mybb_user_data->website;
						$new_wp_userdata['user_email'] = $mybb_user_data->email;
						$new_wp_userdata['display_name'] = $mybb_user_data->username;
						$new_wp_userdata['role'] = get_option('mybb_wp_defaultrole');

						$just_created_wp_user_id = wp_insert_user($new_wp_userdata);
						//user created, let's set wp auth cookie
						wp_set_auth_cookie($just_created_wp_user_id, true, false);
						do_action('wp_login', $user_info->user_login);
						
						if (wp_get_referer()) {
							wp_redirect(wp_get_referer());
						} else {
							wp_redirect( home_url() );
						}
						exit();
					}
					
				}
			}
		}
	}
 }

 
 /**
 * mybb_user_login_nocookie()
 *
 * sets login infos using wp-login.php page.
 * I also need to create mybb cookie and, if first login from this page, update password with a valid one
 *
 */
 function mybb_user_login_nocookie ($username, $password) {
	if( is_user_logged_in() ){ //is user already logged in?
	   
		//return to prev pages
		wp_mybb_cookie_redirect ();
		exit();
	} else {
		global $wpdb;
		$mybb_user_data = $wpdb->get_row("SELECT uid, username, password, salt, loginkey, email, website FROM " . get_option('mybb_wp_user_table') . " WHERE username = '$username'");
		$form_password = md5(md5($mybb_user_data->salt).md5($password));
		
		if ($mybb_user_data->password == $form_password) { //user and password match in mybb db
			//echo "I check if user exists in wordpress database using mybb uid<br>";
			
			if ($wp_user_data = get_user_by( 'email', $mybb_user_data->email ) ){
				//echo "update user password in wp if necessary ...";
				if ( $username && wp_check_password( $password, $wp_user_data->data->user_pass, $wp_user_data->ID) ) {
					//echo "password is already up to date, nothing to do";
				} else {
					//echo "update user password with the current one";
					wp_update_user (  array ( 'ID' => $wp_user_data->ID, 'user_pass' => $password ) );
				}
				//echo "...and set both wp and mybb cookies";
				wp_set_auth_cookie($wp_user_data->ID, true, false);
				setcookie(get_option('mybb_wp_cookiename'), $mybb_user_data->uid."_".$mybb_user_data->loginkey, strtotime( '+1 year' ) ); 

				do_action('wp_login', $wp_user_data->user_login);
				wp_mybb_cookie_redirect ();
				exit();
			} else {
				//echo "user not present in wp database, let's create it";
				$new_wp_userdata['user_login'] = $mybb_user_data->username;
				$new_wp_userdata['user_pass'] = $mybb_user_data->password; // the user will never input this password, so its value isn't really meaningful
				$new_wp_userdata['user_url'] = $mybb_user_data->website;
				$new_wp_userdata['user_email'] = $mybb_user_data->email;
				$new_wp_userdata['display_name'] = $mybb_user_data->username;
				$new_wp_userdata['role'] = get_option('mybb_wp_defaultrole');

				$just_created_wp_user_id = wp_insert_user($new_wp_userdata);
				//echo "user created, let's set wp auth cookie";
				wp_set_auth_cookie($just_created_wp_user_id, true, false);
				setcookie(get_option('mybb_wp_cookiename'), $mybb_user_data->uid."_".$mybb_user_data->loginkey, strtotime( '+1 year' ) );
				
				do_action('wp_login', $user_info->user_login);
		die;

				exit();
			}
		}
	}
 }
 
 /**
 * wp_mybb_cookie_redirect()
 *
 * Redirect user after login
 *
 */
 function wp_mybb_cookie_redirect () {
 	if ( isset( $_REQUEST['redirect_to'] ) ) {
		$redirect_to = $_REQUEST['redirect_to'];
		// Redirect to https if user wants ssl
		if ( $secure_cookie && false !== strpos($redirect_to, 'wp-admin') )
			$redirect_to = preg_replace('|^http://|', 'https://', $redirect_to);
	} else if (wp_get_referer()) {
		$redirect_to = wp_get_referer();
	} else {
		$redirect_to = home_url();
	}
	header("Location: ../index.php");

 }
 
 /**
 * wp_mybb_cookie_logout()
 *
 * logout user deleting also mybb cookies
 *
 */
 function wp_mybb_cookie_logout () {
 	setcookie(get_option('mybb_wp_cookiename'), null, strtotime('-1 day'));
	wp_mybb_cookie_redirect ();
	exit();
 }
 
 /**
 * wp_mybb_cookie_remove_add_user()
 *
 * Remove administration "add user" submenu
 *
 */
 function wp_mybb_cookie_remove_add_user () {
   remove_submenu_page( 'users.php', 'user-new.php' );
 }

 /**
 * wp_mybb_cookie_lost_password()
 *
 * If user registration is still enabled in WP, redirect to forum's lost password page
 *
 */
function wp_mybb_cookie_lost_password () {
	$forum_root = get_option('mybb_wp_forumpath');
	wp_redirect( $forum_root. "/member.php?action=lostpw");
}
 /**
 * wp_mybb_cookie_user_register()
 *
 * If user registration is still enabled in WP, redirect to forum's user registration page
 *
 */ 
function wp_mybb_cookie_user_register () {
	$forum_root = get_option('mybb_wp_forumpath');
	wp_redirect( $forum_root. "/member.php?action=register");
}
 
 /**
 * plugin actions
 *
 */ 
 
  add_action('init', 'mybb_user_login_cookie'); //user login using mybb cookie set from mybb forum
  add_action('wp_authenticate', 'mybb_user_login_nocookie', 10, 2 ); //user login from wp-login.php
  add_action('wp_logout', 'wp_mybb_cookie_logout'); //logout user deleting also mybb cookies
  add_action('admin_init', 'wp_mybb_cookie_init' ); 
  add_action('admin_menu', 'wp_mybb_cookie_admin_menu');
  add_action('admin_menu', 'wp_mybb_cookie_remove_add_user'); //remove add user from admin menu
  add_action('lost_password', 'wp_mybb_cookie_lost_password'); //redirect wp lost_password link to mybb's one
  add_action('retrieve_password', 'wp_mybb_cookie_lost_password'); //redirect wp lost_password link to mybb's one
  add_action('password_reset', 'wp_mybb_cookie_lost_password'); //redirect wp lost_password link to mybb's one
  
  add_action('user_register', 'wp_mybb_cookie_user_register'); //redirect wp user_register link to mybb's one
  add_action('register_form', 'wp_mybb_cookie_user_register'); //redirect wp user_register link to mybb's one
  
  //add_filter('admin_header','hide_profile_fields'); //hide fileds in wordpress that should be modified in mybb

 register_activation_hook( __FILE__, 'wp_mybb_cookie_activation' );
 register_deactivation_hook( __FILE__, 'wp_mybb_cookie_deactivation' );
 register_uninstall_hook( __FILE__, 'wp_mybb_cookie_uninstall' );
  
 ?>

Hope this saves you all from modifying the code and remember to make the changes I have sated about with the forums path.

Cheers,
Spudster Wink
Thanks for fixing Smile
I did not thought to the possibility of a blog without the index.php file, LOL

On line 180 and 290, instead of using
header("Location: ../index.php");

It's better to use:

wp_redirect( home_url() );

because home_url() also evaluates the right protocol and the home page accordingly to your theme.

Doing so you fix the redirect loop, but you always redirect to home page the user, even if he is on another page of your blog: not a big issue, but it can be annoying, obviously, this is up to you Smile
No problem glad to help Smile
There are a few other issues which I PM'ed you about but besides that the plugin works great.
I have my own integration working with wordpress and mybb, everything works both visually and at a data level.

Very simple I use DB Auth reloaded for user integration then my own code which outputs the wordpress header sidebar and footer to HTML files and then i include those into mybb with a plugin

Pretty seemless really, you can see it all working and in action at http://www.silvertails.net (and /forum)

I have almost completed a version of this I want to release but its not perfect for all wordpress themes, it will however get you 90% of the way there visually
(2014-06-03, 02:55 PM)Alder Wrote: [ -> ]
(2014-06-02, 05:56 PM)forumfreund Wrote: [ -> ]Hello Alder,
do you still work on the plugin? Will you update it?

thanks

No, I'm not actively working on it.
I'm using the plugin in a production environment since november and it works fine, so for me it's ok.
I will update it only if needed to let it be compatible with new versions of wordpress and mybb.
Do you need any feature?

(2014-06-02, 06:42 PM)Heroes Killer Wrote: [ -> ]Thank you for fast reply, I'll try to merge those databases... Smile

As far as web and forum location is concerned, it is little more difficult. Big Grin

Web is located in: /www/domains/my-site
Forum is located in: /www/subdomains/forum

URLs are: my-site.com & forum.my-site.com

Uhm…never tried a configuration like that.
Try to install it in a test environment after merging the databases and give me information about the cookies, so I can make further assumptions.

If you can update this for 1.8 that would be awesome Big Grin
Tested and it works with mybb 1.8 and wp 4.0.
Pages: 1 2 3