Coding

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

About Bowe Frankema

 Manage group creation by creating a group request form Building communities on BuddyPress is something I’m very passionate about. The decision to focus exclusively on BuddyPress projects over the last few years inspired me to share back some of the things I've learned here on BP-Tricks! You can find me on WordCamps & BuddyCamps across the globe and on Twitter! Follow me on Twitter

I'm constantly updating my BuddyPress Hosting Guide and SiteGround and WPEngine
have proven to be a great fit for hosting your BuddyPress community.

Need some help finding the perfect host? Check out my in-depth BuddyPress Hosting guide!

Discussion

12 responses to ‘Manage group creation by creating a group request form

  1. 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. :-)

  2. 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.

  3. 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

  4. 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!

  5. 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.

  6. 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

  7. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>