如何編輯群組後設資料教程

如何編輯群組後設資料教程

Codex Home → BuddyPress 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:


<input id="favorite-color" type="text" name="favorite-color" value="" />

<?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 "

My favorite color is:" . custom_field('favorite-color') . "

";
}
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:


<input id="favorite-color" type="text" name="favorite-color" value="" />

<?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 "

My favorite color is:" . custom_field('favorite-color') . "

";
}
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

發表回覆

您的電子郵箱地址不會被公開。 必填項已用 * 標註