Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save KaineLabs/132eeee77a9f89080dc900f81630df67 to your computer and use it in GitHub Desktop.

Select an option

Save KaineLabs/132eeee77a9f89080dc900f81630df67 to your computer and use it in GitHub Desktop.
Youzify - BuddyPress Create Custom Groups Fields
<?php
/**
* Custom Group Fields.
*/
function yzc_custom_groups_fields() {
$fields = array(
array( 'title' => 'Meetup Date & Time', 'name' => 'meetup_date', 'type' =>'text' ),
array( 'title' => 'Meetup Country', 'name' => 'meetup_country', 'type' =>'text' ),
array( 'title' => 'Meetup State', 'name' => 'meetup_state', 'type' =>'text' ),
array( 'title' => 'Meetup City', 'name' => 'meetup_city', 'type' =>'text' ),
array( 'title' => 'Meetup Address', 'name' => 'meetup_address', 'type' =>'text' ),
);
return $fields;
}
// Add HTML
add_filter( 'groups_custom_group_fields_editable', 'yzc_add_custom_group_fields_html' );
function yzc_add_custom_group_fields_html() {
// Get Fields
$fields = yzc_custom_groups_fields();
if ( empty( $fields ) ) {
return;
}
global $bp;
foreach ( $fields as $field ) {
// Get Field Key
$name = 'yzc_' . $field['name'];
// Get Field Value
$value = bp_is_current_action( 'create' ) ? '' : groups_get_groupmeta( $bp->groups->current_group->id, $name );
echo '<div class="youzify-group-field-item">';
echo '<label>' .$field['title'] .'</label>';
switch ( $field['type'] ) {
case 'text':
echo '<input type="text" name="' . $name . '" value="' . $value . '">';
break;
case 'number':
echo '<input type="number" name="' . $name . '" value="' . $value . '">';
break;
case 'textarea':
echo '<textarea name="' . $name . '">' . $value . '</textarea>';
break;
}
echo '</div>';
}
}
/**
* Get Groups Info Widget
*/
add_action( 'youzify_after_group_info_tab_content', 'yzc_get_group_info_widget' );
function yzc_get_group_info_widget() {
// Get Fields.
$fields = yzc_custom_groups_fields();
if ( empty( $fields ) ) {
return;
}
global $bp;
?>
<div class="youzify-group-infos-widget">
<div class="youzify-group-widget-title"><i class="fas fa-info"></i>Group Info</div>
<div class="youzify-group-widget-content">
<?php
foreach ( $fields as $field ) {
// Get Field Value
$value = groups_get_groupmeta( $bp->groups->current_group->id, 'yzc_' . $field['name'] );
if ( empty( $value ) ) {
continue;
}
?>
<div class="youzify-group-info-item" style="margin-bottom: 15px;">
<strong class="youzify-group-info-label"><?php echo $field['title']; ?></strong>
<div class="youzify-group-info-value"><?php echo $value; ?></div>
</div>
<?php } ?>
</div>
</div>
<?php
}
// Save Data.
add_action( 'groups_group_details_edited', 'yzc_group_details_save' );
add_action( 'groups_created_group', 'yzc_group_details_save' );
function yzc_group_details_save( $group_id ) {
// Get Fields.
$fields = yzc_custom_groups_fields();
if ( empty( $fields ) ) {
return;
}
// Save Values.
foreach ( $fields as $field ) {
$name = 'yzc_' . $field['name'];
if ( isset( $_POST[ $name ] ) ) {
if ( ! empty( $_POST[ $name ] ) ) {
groups_update_groupmeta( $group_id, $name, $_POST[ $name ] );
} else {
groups_delete_groupmeta( $group_id, $name );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment