Skip to:
Content
Pages
Categories
Search
Top
Bottom
Codex HomeBuddyPress Plugin Development → How to Edit Group Meta Tutorial

How to Edit Group Meta Tutorial

This tutorial will show you how to add a custom field to the registration process as well as the admin->”edit details” section. I will also show you how to print that out into the header and how to echo that data elsewhere.

We are going to do this as a standalone plugin but you could easily add this to your existing plugins or even your functions.php (without the plugin header data)

Firstly create a folder in your wp-contents/plugins folder. Call it whatever you want this plugin to be called. Lets call ours “Favorite Color” Then create a file inside. Mines called favorite_color.php Open that file up and start with:

<?php
/*
Plugin Name: Favorite Color
Plugin URI: http://MyWebsite.com
Description: Let Groups Pick their favorite Color
Version: 1.0
Requires at least: 3.3
Tested up to: 3.3.1
License: GPL3
Author: Your Name
Author URI: http://YourCoolWebsite.com
*/

This is all the fun data that shows up on the plugin activation screen. Theres tons more info you can add, but thats beyond our scope.

Now that we are in the plugin it’s important to check if BuddyPress is activated. You can read a bit how to do that here.

//wrapper function if BP is activated
function bp_group_meta_init() {
   /*Our Cool Code */
}
add_action( 'bp_include', 'bp_group_meta_init' );
/* If you have code that does not need BuddyPress to run, then add it here. */

Now a lot of people will have you do a require inside the function and load the rest of the code separate in a different file. You can do this, it makes it much cleaner, but for this I’m keeping it simple and only needing one file. Look here for the require code.

So Inside the bp_group_meta_init() function lets start adding code.

The first is:

function custom_field($meta_key='') {
//get current group id and load meta_key value if passed. If not pass it blank
return groups_get_groupmeta( bp_get_group_id(), $meta_key) ;
}

Buddypress groups_get_groupmeta by default does not look if it is in a loop (it should) so we create a function that checks the current group ID and passes it to groups_get_groupmeta. If we pass a meta_key it will return the value, if there isn’t a value it will return blank. If we dont send a meta_key it will return an array with all the meta data for that group ID. If we call this outside a group loop it wont work as it wont be able to guess the group ID.

Now lets create a function to display the input field:

// This function is our custom field's form that is called in create a group and when editing group details
function group_header_fields_markup() {
global $bp, $wpdb;
?>
<label for="favorite-color">My Favorite Color</label>
<input id="favorite-color" type="text" name="favorite-color" value="<?php echo custom_field('favorite-color'); ?>" />
<br>
<?php
}

This outputs a label and input field that gets added to the already existing form during registration and editing. That’s why there is no input or other form data.
We created the id and name based upon  whatever we want to name the field. We then call our custom_field() function with that meta data to see if a value already exists. It wont return anything during the signup process but it will afterwards.

// This saves the custom group meta – props to Boone for the function
// Where $plain_fields = array.. you may add additional fields, eg
//  $plain_fields = array(
//      'field-one',
//      'field-two'
//  );
function group_header_fields_save( $group_id ) {
global $bp, $wpdb;
$plain_fields = array(
'favorite-color'
);
foreach( $plain_fields as $field ) {
$key = $field;
if ( isset( $_POST[$key] ) ) {
$value = $_POST[$key];
groups_update_groupmeta( $group_id, $field, $value );
}
}
}

This is a pretty hefty function. It takes the passed data and saves it into the group meta. The reason they use the array and the foreach statement is in the event you want to add multiple fields. It would be a pain to rewrite the code over and over. this way you simply add a comma and a new field name. It then checks to see if there is a post object with the same name. (which if coming from the registration or edit forms we hooked into earlier it will) It then updates the meta accordingly.

Now we hook our functions into BuddyPress:

add_filter( 'groups_custom_group_fields_editable', 'group_header_fields_markup' );
add_action( 'groups_group_details_edited', 'group_header_fields_save' );
add_action( 'groups_created_group',  'group_header_fields_save' );

The first fires any time there is editable fields in BuddyPress and adds our nice input form. The next two are added when any of that data is normally posted and saved.

At this point you should be able to test your plugin. Activate it from the plugin menu and then attempt to create a group. Your field should show up at the bottom. Enter all the data you have and then continue through the process. Once the group is created click admin and look at the edit screen. Your field should be here as well, populated with the data from the registration process.

This next function adds that info to the group header info:

// Show the custom field in the group header
function show_field_in_header( ) {
echo "<p> My favorite color is:" . custom_field('favorite-color') . "</p>";
}
add_action('bp_group_header_meta' , 'show_field_in_header') ;

this hooks into the header action and adds our code to the bottom.

Now you can get the group meta using simply groups_get_groupmeta and echo it out wherever you’d like. Just be sure to pass the group_ID or it wont return anything. You can do what I did and use bp_get_group_id() as long as you know you will be in a group loop. I may come back and add more on how to access that data but if you’ve made it this far you have all the tools you’ll need!

Full Code:

<?php
/*
Plugin Name: Favorite Color
Plugin URI: http://MyWebsite.com
Description: Let Groups Pick their favorite Color
Version: 1.0
Requires at least: 3.3
Tested up to: 3.3.1
License: GPL3
Author: Your Name
Author URI: http://YourCoolWebsite.com
*/
function bp_group_meta_init() {
function custom_field($meta_key='') {
//get current group id and load meta_key value if passed. If not pass it blank
return groups_get_groupmeta( bp_get_group_id(), $meta_key) ;
}
//code if using seperate files require( dirname( __FILE__ ) . '/buddypress-group-meta.php' );
// This function is our custom field's form that is called in create a group and when editing group details
function group_header_fields_markup() {
global $bp, $wpdb;?>
<label for="favorite-color">My Favorite Color</label>
<input id="favorite-color" type="text" name="favorite-color" value="<?php echo custom_field('favorite-color'); ?>" />
<br>
<?php }
// This saves the custom group meta – props to Boone for the function
// Where $plain_fields = array.. you may add additional fields, eg
//  $plain_fields = array(
//      'field-one',
//      'field-two'
//  );
function group_header_fields_save( $group_id ) {
global $bp, $wpdb;
$plain_fields = array(
'favorite-color'
);
foreach( $plain_fields as $field ) {
$key = $field;
if ( isset( $_POST[$key] ) ) {
$value = $_POST[$key];
groups_update_groupmeta( $group_id, $field, $value );
}
}
}
add_filter( 'groups_custom_group_fields_editable', 'group_header_fields_markup' );
add_action( 'groups_group_details_edited', 'group_header_fields_save' );
add_action( 'groups_created_group',  'group_header_fields_save' );

// Show the custom field in the group header
function show_field_in_header( ) {
echo "<p> My favorite color is:" . custom_field('favorite-color') . "</p>";
}
add_action('bp_group_header_meta' , 'show_field_in_header') ;
}
add_action( 'bp_include', 'bp_group_meta_init' );
/* If you have code that does not need BuddyPress to run, then add it here. */
?>

Props to Charl Kruger for his post: http://charlkruger.com/2011/12/07/getting-started-with-buddypress-group-meta

Skip to toolbar