New WordPress Plugin — Auto Add Users To Mingle

I downloaded the Mingle Forum Plugin today and started tooling around with it. After working through a number of small configuration problems, I discovered that you have to manually allow users to post in forums. Since the plugin is actually really great, I didn’t want to scrap it despite this missing functionality.

It didn’t seem like it should be “that hard” to figure out the database tables that handled the user-group relationship and create a WordPress plugin that would automatically join a new user to the appropriate tables. I’m not sure why, exactly, I assumed this — but that’s the premise I was working off of. The following is the actual plugin and details for anyone that wants to implement it as-is. There’s plenty more work that would need to be done to “release it”.

It’s important to mention that EVERY user that logs into your WordPress instance will be added to a SINGLE group. There’s no way to exclude any users (without adding code for that explicit purpose). This isn’t a problem for my implementation, but I can understand that this may be an issue for some people. Either way, I’m sure this should be adequate for most people’s use.

<?php
/*
Plugin Name: Auto Add Users To Mingle
Plugin URI: http://mikebrum.com/
Description: This plugin automatically joins any user to a defined group ID so they can make forum posts
Version: 0.1
Author: Mike Brum
Author URI: http://mikebrum.com/
License: Freestuffz
*/

/*
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. Please test it in a way that will not
expose you to the loss of data if any problems are encountered.
*/

class forumAdder {
function add2forum() {
global $current_user, $wpdb;
$current_user = wp_get_current_user();
$cuid = $current_user->ID;

# check db to see if they’re part of “the group”
$id_query = “SELECT id, user_id FROM wp_mauimtg_forum_usergroup2user WHERE user_id = ‘$cuid’”;
$my_id = $wpdb->get_row($id_query, ARRAY_N);

# if not, then add them
if ( (! $my_id[1] ) && ($cuid != ’0′) && ($cuid != ”) ){
$wpdb->insert( ‘wp_mauimtg_forum_usergroup2user‘,
array( ‘user_id’ => $cuid, ‘group’ => 2 ),
array( ‘%d’, ‘%s’ )
);
}
}
}

$myForumAdder = new forumAdder();
add_action(‘shutdown’, array($myForumAdder, ‘add2forum’));
?>

So the first thing that you’ll need to do is copy the code above to a file and put it in your plugins dir for WordPress. I called it “auto_add_users_to_mingle.php”. Seemed appropriate.

There’s two things that you need to change:

wp_mauimtg_forum_usergroup2user — this is the Mingle ‘forum_usergroup2user’ table. Given the WordPress table prefix for the blog I was added it to is “wp_mauimtg_”, this is how we get this full table name. If you look in your wp-config.php file and look at your $table_prefix variable, it should be obvious what this should be changed to. It’ll basically be: $table_prefix + ‘forum_usergroup2user’

For reference, my exact line from wp-config.php is:

$table_prefix¬† = ‘wp_mauimtg_’;

The other important thing that needs to change is the line:

array( ‘user_id’ => $cuid, ‘group’ => 2 ),

Specifically, the number “2″ (the rest of the line needs to stay exactly the same).

Now, there’s no “easy” way to figure out what this number is supposed to be. You’re going to have to log into your database and figure out what that number is.

Here’s a straight forward way to figure it out (using the table names that I’m looking at as defined by my $table_prefix):

1) look at all entries in the table wp_mauimtg_forum_usergroups and find the group that you want everyone to be added to. There’s a column called “name” that will actually display the group name, so there’s no real guessing involved once you spit out the contents of wp_mauimtg_forum_usergroups.

2) the value in the “id” column is the number that you’re going to want to specify in the above line.

For example, if you have a group called “Family” that you wanted everyone added to and you looked that row up in your wp_mauimtg_forum_usergroups table and found that it had an id of “15″, then the line would need to be changed to:

array( ‘user_id’ => $cuid, ‘group’ => 15 ),

Once you’ve made the 2x table name changes and the 1x number change, you should be ready to activate the plugin.

If you get an error, you’ve probably screwed up the PHP syntax and should fine-tooth-comb the changes you made.

As I mentioned, this is my first plugin, so I’m sure there’s a number of things I could do to improve it. For instance, I’d imagine there’s a way to pull out the value of $table_prefix and do the string concatenation and I could make an admin panel for the plugin that brings up the list of groups in wp_mauimtg_forum_usergroups and lets you select one from a pull-down instead of having to hack at it by hand.

But both of these enhancements are for another day.

I just wanted to contribute the code if it can help others. Hope you have luck implementing it.

You can download a zip file directly: auto_add_users_to_mingle.0.1.zip

Sep 15th, 2011 | Posted in Random
Comments are closed.