Forum

Whether it's a plugin thats causing issues, a theme, or just needed help with styling your site...We can help! Live Person Support for all your Wordpress Needs!

Welcome Guest

Pages: 1
I created a small plug-in to auto-add users to a group
mikebrumPostSep 16 2011, 12:09 AM

Newbie
Posts: 2
Registered:
Sep 16 2011, 05:37 AM
Normal topicI created a small plug-in to auto-add users to a group

I downloaded this plugin today and started tooling around with it. After working through a number of small problems (never did get skins to work), I quickly discovered that I would have to add users to a group manually... and that just wasn't going to happen. Since the plugin is actually really great, I didn't want to scrap it.

I threw together the following plugin (my first one) after a bit of hacking around. I'm going to share the code and some basic implementation, since there ABSOLUTELY going to be changes that you'll need to make if you want to use it.

It's important to mention that EVERY user that logs into WP will be added to a SINGLE group. 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 WP. 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 my WP table prefix is "wp_mauimtg_", this is how we get this full table. 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.



Shrink, Track, Share, Control ... Dominate
joetomatoPostMay 08 2012, 12:08 AM

Newbie
Posts: 2
Registered:
May 08 2012, 06:00 AM
Normal topicRe: I created a small plug-in to auto-add users to a group

THANK YOU THANK YOU THANK YOU!!!

The plugin works exactly as intended, and your instructions were clear and easy to follow. Nice work!

I paired this with the auto-redirect code from http://thomasgriffinmedia.com/blog/2011/06/how-to-redirect-users-after-they-update-their-profiles-in-wordpress/ to make the registration process nice and easy.

Thanks again!

Edit: There's a much easier way to find out the ID of the group you want to add the user to. After creating the group, simply click on the group name to edit the group, and look in the address bar - it will look something like this:

http://www.example.com/wp-admin/admin.php?page=mfgroups&mingleforum_action=usergroups&do=edit_usergroup&usergroup_id=3

the usergroup_id at the very end is the ID you want.

relightpeoplePostMay 23 2012, 06:03 AM

Newbie
Posts: 5
Registered:
May 22 2012, 02:20 PM
Normal topicRe: I created a small plug-in to auto-add users to a group

Hey guys,

thanks for that feature. But I´m not an IT-Guy and I love the forum.

I have the problem to undesrtand, if people are registrating, they occur in my subscriber list of my whole wordpress-account, where i can´t dintinguish between forum subscribers or normal subscriber, neither if it is an author.
I would be more pleased, if you can seperately register only to the forum, and these people will occur in a different database. Furthermore, how will these people be switched automatically the this special usergroup , if they register themselves. Imagine if i would have 1000 subscribers in my wordpress subscriber-list, mixed with other people like authors, etc. So what can i do. These recommandations above i would not install, because i would make an mistake.

thx

Pages: 1
Mingle Forum by Cartpauj | Version: 1.1.0beta | Page loaded in: 0.546 seconds.


Shrink, Track, Share, Control ... Dominate