Title: [Plugin: Auto Group Join] Admin Menus Error
Last modified: August 19, 2016

---

# [Plugin: Auto Group Join] Admin Menus Error

 *  [bcbccouk](https://wordpress.org/support/users/bcbccouk/)
 * (@bcbccouk)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/)
 * When I click the edit links in the admin pages, I get redirected to the primary
   blog Dashboard. Hmm.
 * [http://wordpress.org/extend/plugins/auto-join-groups/](http://wordpress.org/extend/plugins/auto-join-groups/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [noizeburger](https://wordpress.org/support/users/noizeburger/)
 * (@noizeburger)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/#post-1232043)
 * [@bcbccouk](https://wordpress.org/support/users/bcbccouk/):
    I had the same problem.
   I took a look at bn-auto-join-group.php and realised, that some paths were wrong.
   By correcting the paths I even made it work on bp 1.1.1.
 * Here’s my bn-auto-join-group.php
 *     ```
       <?php
       /**
        * Plugin Name: Auto Group Join
        * Plugin Description: Members automatically are joined to groups based on a profile field
        * Author: Brent Layman
        * Author URI: http://buglenotes.com
        * Plugin URI: http://buglenotes.com
        * Version: 1.0
        */
   
       /*
       Place this folder in your plugins folder.  In the admin panel, configure under "Site Admin -> Profile->Group Links"
   
       If you know the profile field name and the group name are not exact matches, use the group_pre_regex and group_post_regex fields.
       	a. For example, say you have a profile field for "Graduation Year" where an option is "1987".  The corresponding group is "USMA 1987".  So, in the
       		group_pre_regex field put "USMA%".  (yes ... put the % into the db field).
       	b. Another example, say you have a profile field for "Type of Fishing" where an option is "Fly".  The corresponding group is "Fly Fishing".  So, in the
       		group_post_regex field put "%Fishing".
       	c. If the profile options exactly match the group names, leave group_pre_regex and group_post_regex blank.
       */
   
       function addHeaderCode() {
          echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/auto-join-groups/css/bn-auto-join-group.css" />' . "\n";
          }
   
       function auto_join_group_check_installed() {
       	global $wpdb, $bp;
   
       	if ( is_site_admin() ) {
       		/* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
       		if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_link'")  ) {
       			$mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD <code>group_link</code> BINARY NOT NULL DEFAULT '0'";
       			$wpdb->query($mysql);
       		}
       		if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_pre_regex'")  ) {
       			$mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD <code>group_pre_regex</code> varchar(100) NOT NULL";
       			$wpdb->query($mysql);
       		}
       		if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_post_regex'")  ) {
       			$mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD <code>group_post_regex</code> varchar(100) NOT NULL";
       			$wpdb->query($mysql);
       		}
       	}
       }
       add_action('admin_head', 'addHeaderCode');
       add_action( 'admin_menu', 'auto_join_group_check_installed' ); 
   
       // Now that we have the fields we need, let's do the admin menu
       function auto_join_group_plugin_menu() {
         add_submenu_page( 'wpmu-admin.php', __("Link Profile->Groups", 'buddypress'), __("Profile->Groups Link", 'buddypress'), 1, __FILE__, "auto_join_group_plugin_options" );
       }
   
       // Here's the actual admin menu
       function auto_join_group_plugin_options() {
       	global $wpdb, $bp;
   
       	if ( isset($_GET['pre_regex']) && isset($_GET['post_regex']) && isset($_GET['mode']) && isset($_GET['field_id']) && 'save' == $_GET['mode'] ) {
       		// save the changes to the database!
       		if (isset($_GET['link'])) { $group_link = 1; } else { $group_link = 0; }
   
       		$sql = $wpdb->prepare("UPDATE {$bp->profile->table_name_fields} SET group_link = %d, group_pre_regex = %s, group_post_regex = %s WHERE id = %d", $group_link, $_GET['pre_regex'], $_GET['post_regex'], $_GET['field_id']);
       		if ( !$wpdb->query($sql) ) {
       			echo "<strong>Update Failed (or no fields were changed)</strong><hr />";
       		} else {
       			echo "<strong>Update Successful!</strong><hr />";
       			// Now ... let's link existing users to the group!!!!
       				// get list of users
       				if (isset($_GET['link'])) {
       					$users = $wpdb->get_results("SELECT user_id FROM {$bp->profile->table_name_data} WHERE field_id = " . $_GET['field_id']);
       					foreach ($users as $user) {
       						bn_auto_group_join($user->user_id, 'myid');
       					}
       				}
   
       		}
       	} 
   
       	if ( isset($_GET['mode']) && isset($_GET['field_id']) && 'edit' == $_GET['mode'] ) {
       		// edit one of the profile linking options
       		$profiles = $wpdb->get_results("SELECT name, id, group_link, group_pre_regex, group_post_regex FROM {$bp->profile->table_name_fields} WHERE id = " . $_GET['field_id']);
       		?>
       		<form method="get" name="auto-join" action="wpmu-admin.php">
       		<input type="hidden" name="field_id" value="<?php echo $_GET['field_id']; ?>">
       		<input type="hidden" name="mode" value="save">
       		<input type="hidden" name="page" value="auto-join-groups/bn-auto-join-group.php">
       		<table class="form-table"><?php
       		foreach ($profiles as $profile)  { ?>
       			<tr><td><strong>Profile Field Name:</strong></td><td align="left"><strong><?php echo $profile->name; ?></strong></td></tr>
       			<tr><td>Link to Groups:</td><td align="left"><input type="checkbox" name="link" <?php if ($profile->group_link == 0) { echo "No"; } else { echo "checked"; } ?>></td></tr>
       			<tr><td>Pre Matching:</td><td align="left"><input type="text" name="pre_regex" maxlength="100" size="15" value="<?php echo $profile->group_pre_regex; ?>"></td></tr>
       			<tr><td>Post Matching:</td><td align="left"><input type="text" name="post_regex" maxlength="100" size="15" value="<?php echo $profile->group_post_regex; ?>"></td></tr>
       			<tr><td colspan="2" align="left"><input type="submit" value="Save"></td></tr>
       			<tr><td colspan="2" align="left"><strong>DIRECTIONS</strong><br />
       			<strong>1.</strong> Check the "Link to Groups" box if you want to try to automatically join members to groups based on this profile field.<br /><br />
       			<strong>2.</strong> If you know the profile field name and the group name are not exact matches, use the group_pre_regex and group_post_regex fields.<br />
       				&nbsp;&nbsp;<strong>a.</strong> For example, say you have a profile field for "Graduation Year" where an option is "1987".  The corresponding group is "USMA 1987".  So, in the
       					"Pre Matching" field put "USMA%".  (yes ... put the % into the db field).<br />
       				&nbsp;&nbsp;<strong>b.</strong> Another example, say you have a profile field for "Type of Fishing" where an option is "Fly".  The corresponding group is "Fly Fishing".  So, in the
       					"Post Matching" field put "%Fishing".<br />
       				&nbsp;&nbsp;<strong>c.</strong> If the profile options exactly match the group names, leave "Pre Matching" and "Post Matching" blank.<br /><br />
       			<strong>3.</strong> Once you save your changes, if the "Link to Groups" box is checked, all existing wpmu members will be added to the matching groups!
       			</td></tr>
       		 <?php
       		}
       		?></table><?php
       	} else {
       		// get profile fields with group linking
       		$profiles = $wpdb->get_results("SELECT name, id, group_link, group_pre_regex, group_post_regex FROM {$bp->profile->table_name_fields} WHERE parent_id = 0 AND (type = 'selectbox' OR type = 'textbox')");
       		echo "<table class=\"widefat\"><thead><tr><td colspan='4''><strong>Click edit</strong> on the Profile field you'd like to link to a group.<br /><br />
       			<strong>NOTE</strong> - currently only 'selectbox' and 'textbox' type profile fields are supported!</td></tr>
       			<tr><td colspan='5'>&nbsp;</td></tr>
       			<tr><th>Profile Field Name</th><th>Linked?</th><th>Pre Matching</th><th>Post Matching</th><th>&nbsp;</th></tr>";
       		foreach ($profiles as $profile)  { ?>
       			<tr>
       				<td><?php echo $profile->name; ?></td>
       				<td style="text-align:center;"><?php echo ($profile->group_link == 1) ? '<strong>YES</strong>' : 'No'; ?></td>
       				<td><?php echo $profile->group_pre_regex; ?></td>
       				<td><?php echo $profile->group_post_regex; ?></td>
       				<td><a href="wpmu-admin.php?page=auto-join-groups/bn-auto-join-group.php&field_id=<?php echo $profile->id; ?>&mode=edit"><?php _e( 'Edit', 'buddypress' ); ?></a></td>
       			</tr>
       			 <?php
       		}
       		echo "</table>";
       	}
       }
   
       add_action('admin_menu', 'auto_join_group_plugin_menu');
   
       // automatically join users to groups based on profile fields when the user modifies his profile or activates his account
       function bn_auto_group_join($userid, $x = 0, $y = 0){
       	global $wpdb, $bp, $user_id;
       	// get the user id
       	if ($bp->loggedin_user->id && $x !== 'myid') {
       		$userid =  $bp->loggedin_user->id ; // current user if logged in.  On activation, userid sent with do_action
       	}
   
       	// get profile fields with group linking
       	$profiles = $wpdb->get_results("SELECT * FROM {$bp->profile->table_name_fields} WHERE group_link = 1");
   
       	foreach ($profiles as $profile)  {
   
       		// see what the person has for that field
       		$profileinfo = $wpdb->get_results("SELECT value FROM {$bp->profile->table_name_data} WHERE user_id = $userid AND field_id = $profile->id");
       		foreach ($profileinfo as $profilevalue ) {
   
       			// see if we can match the group
       			$groupmatches = $wpdb->get_results("SELECT * FROM {$bp->groups->table_name} WHERE name like '$profile->group_pre_regex$profilevalue->value$profile->group_post_regex'");
       			foreach ($groupmatches as $groupmatch) {
   
       				// see if the user is already in the group
       				if ( !BP_Groups_Member::check_is_member( $userid, $groupmatch->id ) ) {
       					// make sure the user isn't banned from the group!
       					if ( !groups_is_user_banned( $userid, $group->id ) ) {
       						// add the group already!
       							$group_id = $groupmatch->id;
       							$user_id = $userid;
       						      if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
       								groups_delete_invite( $user_id, $group_id );
       								}
   
       							 $new_member = new BP_Groups_Member;
       							 $new_member->group_id = $group_id;
       							 $new_member->inviter_id = 0;
       							 $new_member->user_id = $user_id;
       							 $new_member->is_admin = 0;
       							 $new_member->user_title = '';
       							 $new_member->date_modified = time();
       							 $new_member->is_confirmed = 1;
   
       							 if ( !$new_member->save() ) {
       							 	return false;
       								}
   
       							// Should I add this to the activity stream?  left off for now
   
       							/* Modify group meta */
       							groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
       							groups_update_groupmeta( $group_id, 'last_activity', time() );
   
       					}
   
       				}
       			}
       		}
   
       	}
   
       }
       // below hook is located in bp-xprofile.php.  Add user to group when profile is edited
       add_action( 'xprofile_updated_profile', 'bn_auto_group_join', 1, 1 );
       // below hook is located in wpmu-functions.php.  Add user to group when account is activated
       add_action( 'wpmu_activate_user', 'bn_auto_group_join', 12, 3 );
   
       ?>
       ```
   
 *  [westpointer](https://wordpress.org/support/users/westpointer/)
 * (@westpointer)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/#post-1232059)
 * Hey Gang – sorry for the issue above. I didn’t understand how my plugin would
   get renamed when I loaded it to WP depository. I need to put my training wheels
   back on!
 * I’ve updated the plugin to work correctly with the latest wpmu and bp (1.1.x)
   versions. Please see [bn-auto-join-group](http://brentandmary.net/2009/10/24/bn-auto-join-group-plugin/)
   to get it until I can get my svn updated.
 *  [Rian Rietveld](https://wordpress.org/support/users/rianrietveld/)
 * (@rianrietveld)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/#post-1232081)
 * [@noizeburger](https://wordpress.org/support/users/noizeburger/) Thanks!
 *  [brian-hatano](https://wordpress.org/support/users/brian-hatano/)
 * (@brian-hatano)
 * [16 years ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/#post-1232102)
 * When I go to “edit” a field to include it in Auto Join, I get a 404…
    Using WP
   3.0 RC1.
 *  [Lee Strasheim](https://wordpress.org/support/users/leemanade/)
 * (@leemanade)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/#post-1232103)
 * [@brian](https://wordpress.org/support/users/brian/) Hatano:
 * I found that WP 3.0 changed the file wp-admin.php to ms-admin.php. I changed 
   those references and a couple of incorrect directory references, and got the 
   plugin to work!
 * Install the plugin from the admin->plugin. Make sure it’s deactivated and edit
   it. Delete the current code and copy and paste this code in. Click the Update
   button and then activate the plugin.
 * [**Mod Note:** Code segment removed. If you want to post large blocks of code,
   please use a [pastebin](http://wordpress.pastebin.ca/) [(Alternate URL)](http://wordpress.pastebin.com/).(
   I realise there is another code segment about, but that’s months’ old now, so
   missed the boat on that one.]
 *  [Vera Schafer](https://wordpress.org/support/users/vschafer/)
 * (@vschafer)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/#post-1232106)
 * Just installed this plugin under a brand new WP 3.0 running the latest buddypress
   version but I just can’t figured out how to configure it because I don’t see “
   Profile -> Groups Link under the Site Admin section”. Is that because I don’t
   have WPMU as well?
 *  [Kenneth Henderson](https://wordpress.org/support/users/keneticimages/)
 * (@keneticimages)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/#post-1232111)
 * Any hope in getting this plugin working again? I could really use it.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘[Plugin: Auto Group Join] Admin Menus Error’ is closed to new replies.

## Tags

 * [bugs](https://wordpress.org/support/topic-tag/bugs/)

 * 7 replies
 * 8 participants
 * Last reply from: [Kenneth Henderson](https://wordpress.org/support/users/keneticimages/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/plugin-auto-group-join-admin-menus-error/#post-1232111)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
