Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Buddypress Group meta fields
- Plugin URI: http://MyWebsite.com
- Description: extra group meta fields for buddypress
- 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;
- ?>
- <?php $country_list = array( "country1_key" => "country1_value",
- "country2_key" => "country2_value",
- "country3_key" => "country3_value" );
- ?>
- <label for="country">country</label> <select id="country" value="<?php echo custom_field('country') ?>">
- <?php
- $setting = groups_get_groupmeta( $group_id, 'group_extension_country' );
- foreach($country_list as $country_k => $country_v){
- if($setting == $country_k)
- $selected = 'selected="selected"';
- else
- $selected = "";
- echo '<option value="'.$country_k.'" '.$selected.'>'.$country_v.'</option>';
- }
- ?>
- </select>
- <label for="Website url">url</label>
- <input id="url" type="text" name="url" value="<?php echo custom_field('url'); ?>" />
- <?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(
- 'country','url'
- );
- 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> Country:" . custom_field('country') . "</p>";
- echo "<p> Site URL:" . custom_field('url'). "</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. */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement