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() ) : ?> <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') ) : ?> <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() ):?> <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"/> 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à
- - - - - - - - You've finished reading Manage group creation by creating a group request form on BP-Tricks.