Ask your WordPress questions! Pay money and get answers fast! (more info)

Adding more fields to Buddypress Groups WordPress

  • SOLVED

Hi,

I would like to add more specific fields to a group in buddypress. Besides description be able to add street address, mobile, phone and website. Embedded in a h5 tag. And only be visible if you fill in those field. Also so the rich editor is available in groups. Below is the documentation for extending groups.

https://codex.buddypress.org/developer/group-extension-api/

I know there is plugin https://wordpress.org/support/view/plugin-reviews/buddypress-groups-extras but it's way more that I want. Also, I'm trying to get rid of plugins.. For getting down requests..

Answers (1)

2014-12-04

Bob answers:

Please put below code in your theme's functions.php file

Note code is not tested. But it's just idea how it should be written.

function bob_add_group_extension() {
if ( class_exists( 'BP_Group_Extension' ) ) :

class Group_Extension_Ville extends BP_Group_Extension {

function __construct() {
$args = array(
'slug' => 'add-address',
'name' => 'Address',
);
parent::init( $args );
}

function display() {
$group_id = bp_get_group_id();
$st_address = groups_get_groupmeta( $group_id, 'group_ext_st_address' );
$mobile = groups_get_groupmeta( $group_id, 'group_ext_mobile' );
$phone = groups_get_groupmeta( $group_id, 'group_ext_phone' );

if($st_address)
echo "<h5>$st_address<h5>";
if($mobile)
echo "<h5>$mobile<h5>";
if($phone)
echo "<h5>$phone<h5>";

}

function settings_screen( $group_id = NULL ) {
$st_address = groups_get_groupmeta( $group_id, 'group_ext_st_address' );
$mobile = groups_get_groupmeta( $group_id, 'group_ext_mobile' );
$phone = groups_get_groupmeta( $group_id, 'group_ext_phone' );
?>
<div>
Street Address:<br />
<input type="text" name="group_ext_st_address" value="<?php echo $st_address; ?>">
</div>
<div>
Mobile:<br />
<input type="text" name="group_ext_mobile" value="<?php echo $mobile; ?>">
</div>
<div>
Phone:<br />
<input type="text" name="group_ext_phone" value="<?php echo $phone; ?>">
</div>
<?php
}

function settings_screen_save( $group_id = NULL ) {

if ( isset( $_POST['group_ext_st_address'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_st_address', sanitize_text_field($_POST['group_ext_st_address']) );
}
if ( isset( $_POST['group_ext_mobile'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_mobile', sanitize_text_field($_POST['group_ext_mobile']) );
}
if ( isset( $_POST['group_ext_phone'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_mobile', sanitize_text_field($_POST['group_ext_phone']) );
}
}
}
bp_register_group_extension( 'Group_Extension_Ville' );



endif; // if ( class_exists( 'BP_Group_Extension' ) )
}
add_action('bp_init', 'bob_add_group_extension');


Please try it and let me know.


Veritus comments:

Thank You, I have extending it and sent you a message on Skype.


Bob comments:

Here is modified display function to show link in website and email id.

function display() {
$group_id = bp_get_group_id();
$st_address = groups_get_groupmeta( $group_id, 'group_ext_st_address' );
$mobile = groups_get_groupmeta( $group_id, 'group_ext_mobile' );
$phone = groups_get_groupmeta( $group_id, 'group_ext_phone' );
$website = groups_get_groupmeta( $group_id, 'group_ext_website' );
$email = groups_get_groupmeta( $group_id, 'group_ext_email' );


if($st_address)
echo "<h5>$st_address<h5>";

if($mobile)
echo "<h5>$mobile<h5>";

if($phone)
echo "<h5>$phone<h5>";

if($website)
echo '<a href="'.$website.'">'.$website.'</a>';

if($email)
echo '<a href="mailto:'.$email.'">'.$email.'</a>';
}


Bob comments:

full code

<?php
/* ------------------------------------------------------------
:: ADD GROUPS EXTENSIONS
--------------------------------------------------------------- */
function bob_add_group_extension() {
if ( class_exists( 'BP_Group_Extension' ) ) :
class Group_Extension_Ville extends BP_Group_Extension {
function __construct() {
$args = array(
'slug' => 'add-contact-info',
'name' => 'Contact Info',
);
parent::init( $args );
}
function display() {
$group_id = bp_get_group_id();
$st_address = groups_get_groupmeta( $group_id, 'group_ext_st_address' );
$country = groups_get_groupmeta( $group_id, 'group_ext_country' );
$mobile = groups_get_groupmeta( $group_id, 'group_ext_mobile' );
$phone = groups_get_groupmeta( $group_id, 'group_ext_phone' );
$email = groups_get_groupmeta( $group_id, 'group_ext_email' );

$website = groups_get_groupmeta( $group_id, 'group_ext_website' );

if($st_address)
echo "<h5>$st_address<h5>";
if($country)
echo "<h5>$country<h5>";
if($mobile)
echo "<h5>$mobile<h5>";
if($phone)
echo "<h5>$phone<h5>";
if($email)
echo '<h5><a href="mailto:'.$email.'">'.$email.'<h5>';

if($website)
echo '<h5><a href="'.$website.'" target="_blank">'.$website.'<h5>';
}
function settings_screen( $group_id = NULL ) {
$st_address = groups_get_groupmeta( $group_id, 'group_ext_st_address' );
$country = groups_get_groupmeta( $group_id, 'group_ext_country' );
$mobile = groups_get_groupmeta( $group_id, 'group_ext_mobile' );
$phone = groups_get_groupmeta( $group_id, 'group_ext_phone' );
$email = groups_get_groupmeta( $group_id, 'group_ext_email' );
$website = groups_get_groupmeta( $group_id, 'group_ext_website' );
?>
<div>
Street Address:<br />
<input type="text" name="group_ext_st_address" value="<?php echo $st_address; ?>">
</div>
<div>
Country:<br />
<input type="text" name="group_ext_country" value="<?php echo $country; ?>">
</div>
<div>
Mobile:<br />
<input type="text" name="group_ext_mobile" value="<?php echo $mobile; ?>">
</div>
<div>
Phone:<br />
<input type="text" name="group_ext_phone" value="<?php echo $phone; ?>">
</div>
<div>
Email:<br />
<input type="text" name="group_ext_email" value="<?php echo $email; ?>">
</div>
<div>
Website:<br />
<input type="text" name="group_ext_website" value="<?php echo $website; ?>">
</div>
<?php
}
function settings_screen_save( $group_id = NULL ) {
if ( isset( $_POST['group_ext_st_address'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_st_address', sanitize_text_field($_POST['group_ext_st_address']) );
}
if ( isset( $_POST['group_ext_country'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_country', sanitize_text_field($_POST['group_ext_country']) );
}
if ( isset( $_POST['group_ext_mobile'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_mobile', sanitize_text_field($_POST['group_ext_mobile']) );
}
if ( isset( $_POST['group_ext_phone'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_phone', sanitize_text_field($_POST['group_ext_phone']) );
}
if ( isset( $_POST['group_ext_email'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_email', sanitize_text_field($_POST['group_ext_email']) );
}
if ( isset( $_POST['group_ext_website'] ) ) {
groups_update_groupmeta( $group_id, 'group_ext_website', sanitize_text_field($_POST['group_ext_website']) );
}
}
}
bp_register_group_extension( 'Group_Extension_Ville' );
endif; // if ( class_exists( 'BP_Group_Extension' ) )
}
add_action('bp_init', 'bob_add_group_extension');


Bob comments:

Please replace code as below instructions to add labels.
<strong>Old code</strong>

if($st_address)
echo "<h5>$st_address<h5>";
if($country)
echo "<h5>$country<h5>";
if($mobile)
echo "<h5>$mobile<h5>";
if($phone)
echo "<h5>$phone<h5>";
if($email)
echo '<h5><a href="mailto:'.$email.'">'.$email.'<h5>';
if($website)
echo '<h5><a href="'.$website.'" target="_blank">'.$website.'<h5>';


<strong>New Code</strong>

if($st_address)
echo "<h5><span class='contact-label'>Street address:<span>$st_address<h5>";
if($country)
echo "<h5><span class='contact-label'>Country:<span>$country<h5>";
if($mobile)
echo "<h5><span class='contact-label'>Mobile:<span>$mobile<h5>";
if($phone)
echo "<h5><span class='contact-label'>Phone:<span>$phone<h5>";
if($email)
echo '<h5><span class="contact-label">Email:<span><a href="mailto:'.$email.'">'.$email.'<h5>';
if($website)
echo '<h5><span class="contact-label">Website:<span><a href="'.$website.'" target="_blank">'.$website.'<h5>';



**remove extra spaces paste it( if any )
** you need to write css for the class "contact-label" so each label has same width and it can be displayed as you want.


Veritus comments:

Thank you bob, I'll check this out!