Manage group creation by creating a group request form

If you want to simply manage group creation on your BuddyPress site, this Trick will show you how to add a form that will send an email containing a request from the member. To achieve this we’ll need to modify a 2 template files of your active (child)theme and put some some code in the functions.php file. Let’s get started!

Step 1 : Add two templates to your Child Theme

Copy the following folder from the BP-Default Theme to your Child Theme:

/groups/index.php
/groups/create.php.

So you should now have a folder called Groups in your Child Theme, with the files index.php and create.php in it

Step 2: Edit index.php

In index.php on line 7 replace:

<h3><?php _e( 'Groups Directory', 'buddypress' ) ?><?php if ( is_user_logged_in() ) : ?> &nbsp;<a class="button" href="<?php echo bp_get_root_domain() . '/' . BP_GROUPS_SLUG . '/create/' ?>"><?php _e( 'Create a Group', 'buddypress' ) ?></a><?php endif; ?></h3>

with

<h3><?php _e( 'Groups Directory', 'buddypress' ) ?><?php if ( current_user_can('manage-options') ) : ?> &nbsp;<a class="button" href="<?php echo bp_get_root_domain() . '/' . BP_GROUPS_SLUG . '/create/' ?>"><?php _e( 'Create a Group', 'buddypress' ) ?></a><?php elseif ( is_user_logged_in() ):?> &nbsp;<a class="button" id="imath_req_gp_button" href="#group-request">Request a Group</a><?php endif; ?></h3>

Step 3: Edit Create.php

What we are going to to is hide the Group Creation form for regular members, and only make it viewable for logged in Admins. To do so we just add the following code just before the tag form

<?php if(current_user_can('manage-options')):?>

Then after the closing tag of the form around line 255, add this :

<?php else:?>
	<div id="message" class="error fade"><p>You need to request the Admin to create your group !</p></div>
<?php endif;?>

Step 4 : Adding some code to functions.php

Simply paste in the following code :

//request group functions
function imath_req_gp_add_request_form(){
	if(!current_user_can('manage-options')){
		?>
		<div id="group_request_container">
			<div id="message" class="error fade"></div>
				<p><label for="_user_group_name">What will be the name of your group ?</label>
				<input type="text" id="_user_group_name"/></p>
				<p><label for="_user_group_desc">Give a short description of your Group</label><br/>
				<textarea id="_user_group_desc"></textarea></p>
				<span id="gp_req_button_container"><input type="button" id="_confirm_request_button" value="Confirm Request"/></span>
		</div>
		<?php
	}
}
add_action('bp_before_directory_groups_content','imath_req_gp_add_request_form');
function imath_req_gp_handle_ajax(){
	global $bp;
	$to = get_option('admin_email');
	$subject = "new group request in ".get_option('blogname');
	$message = "Hi Admin,n".$bp->loggedin_user->userdata->user_login." wants to manage a group !nnGroup name:n ".stripslashes($_POST['group_slug'])."nnGroup description: n".stripslashes($_POST['group_desc'])."n________________nn";
	$message.= "To view his/her profile: ".$bp->loggedin_user->domain."nTo send him an email: ".$bp->loggedin_user->userdata->user_email;
	if(wp_mail( $to, $subject, $message) ){
		echo "ok";
	}
	else echo "ko";
	die();
}
add_action('wp_ajax_imath_req_gp_ajax_call', 'imath_req_gp_handle_ajax');
function imath_req_gp_load_style_js(){
	if(bp_is_page( BP_GROUPS_SLUG )){
		?>
		<style>
			div#group_request_container{
				width:60%;
				float:left;
				padding-bottom:20px;
				display:none;
			}
			div#group_request_container input[type=text], div#group_request_container textarea{
				border: 1px inset #ccc;
				-moz-border-radius: 3px;
				-webkit-border-radius: 3px;
				border-radius: 3px;
				padding: 6px;
				font: inherit;
				font-size: 14px;
				color: #888;
				width:400px;
			}
			div#group_request_container textarea{
				height:100px;
			}
		</style>
		<script type="text/javascript">
			function js_send_request(){
				var oldhtml = jQuery("#gp_req_button_container").html();
				jQuery("#gp_req_button_container").html('<img src="<?php echo get_bloginfo('template_url');?>/_inc/images/ajax-loader.gif"/>&nbsp;Please wait while sending your request...');
				var data = {
					action: 'imath_req_gp_ajax_call',
					group_slug: jQuery("#_user_group_name").val(),
					group_desc: jQuery("#_user_group_desc").val()
				};
				jQuery.post(ajaxurl, data, function(response) {
					if(response!="ko"){
						jQuery("#group_request_container").html('<div id="message" class="updated fade"><p>Request sent, Admin will shortly keep you informed about it..</p></div>');
					}else{
						jQuery("#gp_req_button_container").html(oldhtml);
						jQuery("#group_request_container #message").html('<p>OOps something went wrong, please try again..</p>');
					}
				});
			}
			jQuery(document).ready(function(){
				jQuery("#imath_req_gp_button").click(function(){
					jQuery("#group_request_container").slideToggle();
				});
				jQuery("#_confirm_request_button").click(function(){
					if(jQuery("#_user_group_name").val().length < 1){
						alert("Please choose a group name");
					}
					else if(jQuery("#_user_group_desc").val().length < 1){
						alert("Please add a short description of your group");
					}
					else{
						js_send_request();
					}
				});
			});
		</script>
		<?php
	}
}
add_action('wp_head', 'imath_req_gp_load_style_js');

What we’ve done now, is created a custom form that regular members need to fill in, if they want a BuddyPress group to be created. When you receive a request you can then manually create the Group, and make the person who made the request the Group Admin.

Voilà icon smile Manage group creation by creating a group request form

BuddyPress Hosting Guide Looking for quality BuddyPress hosting? We will guide you through all the available hosting options and help you find the best webhost for your next BuddyPress project!

12 Responses to Manage group creation by creating a group request form

  1. imath January 5, 2011 at 9:29 pm #

    I think this plugin http://wordpress.org/extend/plugins/bp-admin-actions/ by slaFFik can be useful to add the member to the group once created..

  2. Wendell August 16, 2011 at 6:57 pm #

    This is not working in BP 1.5b2. When clicking the “Create Group” button, regardless of whether you are a regular user or admin, you get the red message stating you have to request the group from the Admin. Also, the request form does not appear.

    Thank you for this site, I really appreciate it and look forward to your tricks for BP 1.5. :-)

  3. Mistafo August 22, 2011 at 3:05 pm #

    when I add this it works great but I don’t see the create group button when I view it being the admin. Any ideas why? I can’t create groups at all with this enabled.

  4. Igor Longagnani January 16, 2012 at 4:09 am #

    I managed to make Create Button appear when I am logged as WP Admin using the WP function
    current_user_can(‘administrator’)
    instead of
    current_user_can(‘manage-options’)
    still e-mail is not sent by normal users correctly, still checking why

    • E.J. June 5, 2012 at 3:46 am #

      Did you ever figure this out? Im getting the same issue.

      Thanks!

  5. dunhakdis March 12, 2012 at 8:19 am #

    I really appreciate this work and in fact in works like a charm for me! however I don’t understand why people is coding this way, I would love to see css alone in .css file and html alone with some of your functions API in ‘view’ and pure php script in functions.php. anyways this is just my personal thought,

    Again, thank you so much!

  6. John April 20, 2012 at 4:12 pm #

    This would be great to automate. Is there anyway to automate the group creation? Lets say a user wants to create a group associated with a particular custom post. It would be cool if they could click create group from the front end and it would automatically create a group linked to their custom post.

  7. Josh Craig April 30, 2012 at 1:19 pm #

    Thanks for the tutorial.

    But, I am having an issue.

    The Admin should be able to click on “Create Group” and create a group as normal. But in my case when I click “Create Group” I get the Red warning box, but the form does not show up.

    Second issue, is that when I’m logged into the site as a normal member when I click on create group the group page just reloads instead of showing the error message and the request a group form.

    I followed the steps exactly, is there anything I might have missed, or code that I can change?

    Thanks

  8. imath July 30, 2012 at 8:51 pm #

    Hello,

    Sorry for my late reply, i’ve modified the script to make it work in BuddyPress 1.5+ and packed it in a child theme.. You can download it from my dropbox.

    Hope it will fix the issues mentioned.

  9. Thrasher March 8, 2013 at 5:31 am #

    Thanks . Trick is good . :)

Trackbacks/Pingbacks

  1. How to restrict or disable create group button in Buddypress | This Grasya on the road less traveled - November 26, 2011

    […] http://bp-tricks.com/coding/manage-group-creation-by-creating-a-group-request-form/ […]

  2. How to restrict or disable create group button in Buddypress | This Grasya on the Road - December 26, 2012

    […] http://bp-tricks.com/coding/manage-group-creation-by-creating-a-group-request-form/ […]

Leave a Reply