Group Mods

  • Profile picture of @mercime
  • Profile picture of r-a-y
  • Profile picture of Hugo

How to add fields to group creation with Group extension API (please read) (57 posts)

Started 9 months, 1 week ago by: alfredojp

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    I’m pretty good with MySql but not so hot with PHP. I’ve spent two weeks trying to figure out how to add
    another field(s) to the group creation so that a few more fields would be standard for the groups (i already know about that plugin that lets group admin put in fields aftewards), hours and hours and hours on these same forums with little help. I ran across group extension API code ( http://codex.buddypress.org/developer-docs/group-extension-api/ ) but i have no idea how to use it. What do i add, and in what file do i put it in (functions.php, bp-custom.php) so that i can have group fields (at creation) for city, state, country and phone number.

    Any help in this is greatly appreciated.

  • Profile picture of Boone Gorges Boone Gorges said 9 months, 1 week ago:

    You can put the group class into bp-custom.php.

    Put the HTML markup for your fields in the display() method. In the create_screen_save() and edit_screen_save() methods, save the data in the $_POST array using groups_update_groupmeta().

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    Hi @boonebgorges
    I’m not exactly sure how I save the data from the “create_screen_save()” and “edit_screen_save()” methods in the $_post. I know you’re busy but could you help me out with an exxample?

    Thank you for the help….i’m going to try what you just said to the best of my abilities

    I also ran across the post which you helped someone else on about 9 months ago…same basic problem.
    Here’s the link:

    http://buddypress.org/community/groups/miscellaneous/forum/topic/new-group-creation-fields

    What do you recommend? Any further help is appreciated

  • Profile picture of Boone Gorges Boone Gorges said 9 months, 1 week ago:

    I can show you how I do it in one of my plugins: https://github.com/boonebgorges/buddypress-docs/blob/master/includes/integration-groups.php#L781

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    @boonebgorges
    so from looking at the code you were implementing something for group docs in the group creation process. am I correct? I get kind of confused because the more and more i look at the code the more and more i start to think that its purpose is to add another Screen to the group creation process and not specifically for more fields.
    Correct me if i’m wrong

  • Profile picture of modemlooper modemlooper said 9 months, 1 week ago:

    Yes, it creates a new screen during creation.

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    but what about the fields?

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    @modemlooper did you see the other code i posted above? which one would be more appropriate to follow to create new fields in the group creation process?

    Thank for the confirmation that it creates a new screen. which is cool too but i also need extra fields for city, state, country, phone number and mestre(teacher)

  • Profile picture of modemlooper modemlooper said 9 months, 1 week ago:

    You will have to edit core files or add_action to the creation. I’m pretty sure this has been asked before search google instead of search on this site

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    @modemlooper (if me grabbing your attention this way is rude please let me know)
    Thanks…..but are you referring to the API or to the code i posted when you said i’ll have to edit core files. And yes it has been asked many times before but no one ever seems to complete it.

    Thank you for your time. :)

  • Profile picture of modemlooper modemlooper said 9 months, 1 week ago:

    I’ll find the link and post it but someone even created a plugin to add extra fields below name and description

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    I found one called gField but it doesn’t really work…it adds a location field but doens’t make any change tot he group table in the database.

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    I found one called gField but it doesn’t really work…it adds a location field but doens’t make any change tot he group table in the database.

  • Profile picture of alfredojp alfredojp said 9 months, 1 week ago:

    @boonebgorges
    does the groups API code only create a new screen or can i also use it to create extra fields?

  • Profile picture of Boone Gorges Boone Gorges said 9 months, 1 week ago:

    The groups extension API is meant to add a new tab to the groups interface. If by “create extra fields” you mean that you want to add additional metadata to the Group Details tab, here is some code from a client project I once did. I’ve modified it a bit to remove some custom functions. This should give you an idea of how to start.

    add_filter( 'groups_custom_group_fields_editable', 'group_details_markup' );
    add_action( 'groups_group_details_edited', 'group_details_save' );
    add_action( 'groups_created_group',  'group_details_save' );
    
    function group_details_markup() {
    	$group_links = groups_get_groupmeta( $bp->groups->current_group->id, 'e2e_links' );
    	extract( $group_links );
    
    	$group_text = groups_get_groupmeta( $bp->groups->current_group->id, 'e2e_text' );
    
    	?>
    	<label for="group-text">Restaurant Description</label>
    	<p>(Type of food, etc)</p>
    	<textarea name="group-text" id="group-text"><?php echo $group_text ?></textarea>
    
    	<label for="group-price">Restaurant Price</label>
    	<?php $price = groups_get_groupmeta( $bp->groups->current_group->id, 'e2e_price' ); ?>
    	<select name="group-price">
    		<option value="1" <?php if ( $price == '1' ) : ?>selected="selected"<?php endif ?>>$</option>
    		<option value="2" <?php if ( $price == '2' ) : ?>selected="selected"<?php endif ?>>$$</option>
    		<option value="3" <?php if ( $price == '3' ) : ?>selected="selected"<?php endif ?>>$$$</option>
    		<option value="4" <?php if ( $price == '4' ) : ?>selected="selected"<?php endif ?>>$$$$</option>
    		<option value="5" <?php if ( $price == '5' ) : ?>selected="selected"<?php endif ?>>$$$$$</option>
    	</select>
    
    	<?php $url = groups_get_groupmeta( $bp->groups->current_group->id, 'e2e_url' ); ?>
    
    	<label for="group-url">Restaurant Website</label>
    	<input type="text" name="group-url" id="group-url" value="<?php echo $url ?>" />
    
    	<?php $phone = groups_get_groupmeta( $bp->groups->current_group->id, 'e2e_url' ); ?>
    
    	<label for="group-phone">Restaurant Phone Number</label>
    	<input type="text" name="group-phone" id="group-phone" value="<?php echo $phone ?>" />
    
    	<label for="group-menupages">MenuPages</label>
    	<input type="text" name="group-menupages" id="group-menupages" value="<?php echo $MenuPages ?>" />
    
    	<label for="group-yelp">Yelp</label>
    	<input type="text" name="group-yelp" id="group-yelp" value="<?php echo $Yelp ?>" />
    
    	<label for="group-chow">Chow</label>
    	<input type="text" name="group-chow" id="group-chow" value="<?php echo $Chow ?>" />
    
    	<label for="group-everyblock">Everyblock</label>
    	<input type="text" name="group-everyblock" id="group-everyblock" value="<?php echo $EveryBlock ?>" />
    
    	<?php
    
    	return;
    }
    
    function group_details_save( $group_id ) {
    	global $bp, $wpdb;
    
    	$plain_fields = array(
    		'url',
    		'phone',
    		'text',
    		'price'
    	);
    
    	$links_fields = array(
    		'menupages' => 'MenuPages',
    		'yelp' => 'Yelp',
    		'chow' => 'Chow',
    		'everyblock' => 'EveryBlock'
    	);
    
    	foreach( $plain_fields as $field ) {
    		$key = 'group-' . $field;
    		if ( isset( $_POST[$key] ) ) {
    			$value = $_POST[$key];
    			groups_update_groupmeta( $group_id, 'e2e_' . $field, $value );
    		}
    	}
    
    	$links = array();
    	foreach( $links_fields as $fn => $name ) {
    		$key = 'group-' . $fn;
    		if ( isset( $_POST[$key] ) ) {
    			$links[$name] = $_POST[$key];
    		}
    	}
    	groups_update_groupmeta( $group_id, 'e2e_links', $links );
    
    	// Prevent null searches by entering dummy values for important meta
    	if ( !groups_get_groupmeta( $group_id, 'bpgr_rating' ) )
    		groups_update_groupmeta( $group_id, 'bpgr_rating', 'null' );
    
    	if ( !groups_get_groupmeta( $group_id, 'bpgr_how_many_ratings' ) )
    		groups_update_groupmeta( $group_id, 'bpgr_how_many_ratings', 'null' );
    
    	if ( !groups_get_groupmeta( $group_id, 'e2e_price' ) )
    		groups_update_groupmeta( $group_id, 'e2e_price', 'null' );
    }