Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

BP group extension - return value WordPress

  • SOLVED

Hello there!

I try to use bp extension group api to add a step in registering group.
It works, but so far, it is only an <input type="text"> field.
I would like to have a dropdown menu, but doing so, it does not display the value anymore.

Here is the code:

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

class Group_Extension_Ville extends BP_Group_Extension {

function __construct() {
$args = array(
'slug' => 'group-extension-ville',
'name' => 'ville',
);
parent::init( $args );
}

function display() {
$group_id = bp_get_group_id();
$group_extension_ville = groups_get_groupmeta( $group_id, 'group_extension_ville' );
echo 'Ville: ' . esc_attr( $group_extension_ville );
}

function settings_screen( $group_id = NULL ) {
$setting = groups_get_groupmeta( $group_id, 'group_extension_ville' );
?>
Ville: <input type=”text” name='group_extension_ville' value='<?php echo esc_attr( $setting ) ?>' />
<?php
}

function settings_screen_save( $group_id = NULL ) {
$setting = '';

if ( isset( $_POST['group_extension_ville'] ) ) {
$setting = $_POST['group_extension_ville'];
}

groups_update_groupmeta( $group_id, 'group_extension_ville', $setting );
}
}
bp_register_group_extension( 'Group_Extension_Ville' );

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


Could someone explain how I can have a dropdown menu returning the value + researchable, so it becomes a kind of category.

I need the groups to be listed according to the city.
I would like to have mydomain/groups/locations/paris for example with all the groups listed as Paris.

Many thanks in advance

Answers (1)

2014-10-26

Bob answers:

You need dropdown instead of textbox right?

old code
Ville: <input type=”text” name='group_extension_ville' value='<?php echo esc_attr( $setting ) ?>'

New code

<?php
$ville_list = array( "ville1_key" => "ville1_value",
"ville2_key" => "ville2_value",
"ville3_key" => "ville3_value" );
?>
Ville: <select name="group_extension_ville">
<?php
$setting = groups_get_groupmeta( $group_id, 'group_extension_ville' );
foreach($ville_list as $ville_k => $ville_v){
if($setting == $ville_k)
$selected = 'selected="selected"';
else
$selected = "";
echo '<option value="'.$ville_k.'" '.$selected.'>'.$ville_v.'</option>';
}
?>
</select>
<?php


change the following data as required. It should be ville name and it's code or the same name.
"ville1_key" => "ville1_value",
"ville2_key" => "ville2_value",
"ville3_key" => "ville3_value"


sbdm05 comments:

Hello Bob!
First thank you so much for your help!

It works, i have a new tab in the process with the drop down menu and the value is found in the group profile.

The other part of the question, is how do I make these fields searchable in the group directory? Like a filter.

I would like to have three search fields like country/city/activity and the page would display the matching groups.

I have add money for that question!
Many thanks for your quick reply ! ;-)
So


sbdm05 comments:

Right now, the groups-loops.php is the following:
<?php

/**
* BuddyPress - Groups Loop
*
* Querystring is set via AJAX in _inc/ajax.php - bp_dtheme_object_filter()
*
* @package BuddyPress
* @subpackage bp-default
*/

?>

<?php if ( bp_has_groups( bp_ajax_querystring( 'groups' )."&per_page=". apply_filters('kleo_bp_groups_pp', 12) ) ) : ?>


<?php do_action( 'bp_before_directory_groups_list' ); ?>

<div id="groups-list" class="item-list search-list" role="main">

<?php while ( bp_groups() ) : bp_the_group(); ?>

<div class="four columns">
<div class="search-item">

<div class="avatar">
<a href="<?php bp_group_permalink(); ?>"><?php bp_group_avatar( 'type=thumb&width=94&height=94&class=' ); ?></a>
</div>

<div class="search-meta">
<h5 class="author"><a href="<?php bp_group_permalink(); ?>"><?php bp_group_name(); ?></a></h5>
<p class="date"><?php printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() ); ?></p>
</div>
<div class="search-body">
<div class="item-desc"><?php bp_group_description_excerpt(); ?></div>

<?php do_action( 'bp_directory_groups_item' ); ?>
</div>
<br>
<div class="action">
<?php do_action( 'bp_directory_groups_actions' ); ?>
<div class="meta">
<?php bp_group_type(); ?> / <?php bp_group_member_count(); ?>
</div>
</div>
<br>
</div>
</div>

<?php endwhile; ?>

</div>

<?php do_action( 'bp_after_directory_groups_list' ); ?>

<div id="pag-bottom" class="pagination">

<div class="pag-count" id="group-dir-count-bottom">

<?php bp_groups_pagination_count(); ?>

</div>

<div class="pagination-links" id="group-dir-pag-bottom">

<?php bp_groups_pagination_links(); ?>

</div>

</div>

<?php else: ?>

<div id="message" data-alert class="alert-box">
<?php _e( 'There were no groups found.', 'buddypress' ); ?>
</div>

<?php endif; ?>

<?php do_action( 'bp_after_groups_loop' ); ?>


if it helps! So


Bob comments:

I am not exactly sure how we can achieve this.

I think there should be filter which can help you. And you need meta query to filter content according to user choice.

This link can be helpful to you.
[[LINK href="https://codex.buddypress.org/plugindev/group-meta-queries-usage-example/#filter-bp_ajax_querystring-to-eventually-extend-the-groups-query"]]https://codex.buddypress.org/plugindev/group-meta-queries-usage-example/#filter-bp_ajax_querystring-to-eventually-extend-the-groups-query
[[/LINK]]

Above link shows how you can filter data with meta query. The filter used is <strong>bp_ajax_querystring </strong>

At the very bottom you will find a code which adds new value to filter dropdown.

Is you buddypress enabled site live or you are working in localhost?


sbdm05 comments:

Hello Bob, thank you for your response.
I have seen this article before, but iam afraid, my skills wont let me use it!

Iam working live right now.

Is it something you can help me with?

Many thanks in advance,

So


Bob comments:

Can you create beta version of it.

It would be better if we chat on skype or gtalk.

my skype id is : thevaghela

and gmail is : [email protected]


Bob comments:

I need to sleep now.

i will take a look at above problem later.


sbdm05 comments:

OK thank you so much Bob, i will email you my details,

So


Bob comments:

Hi,

I checked your files.

I think I can write this code.

The main page has and extra dropdown. User will select an item from dropdown. the group-loop page is supplying meta query to main argument of buddypress group query.

Just confirm that you need the dropdown after the search box, near the sort by dropdown.

Regards
Bob