Opened 9 years ago
Last modified 9 years ago
#7304 new defect (bug)
xProfile Group field
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 2.7 |
| Component: | Extended Profile | Keywords: | reporter-feedback |
| Cc: | danco38@… |
Description (last modified by )
I'm unable to hide a profile field group from the profile edit screen by using following function. It worked in the past, at least 2.1.
Not sure it is a bug, but if somebody could confirm.
The issue was mentionned on support:
https://buddypress.org/support/topic/hide-xprofile-fields-from-edit/
function bpfr_hide_profile_field_group( $retval ) {
if ( bp_is_active( 'xprofile' ) ) :
// hide profile group/field to all except admin
//if ( !is_super_admin() ) {
// hide fields & group tabs on edit screen
if( bp_is_user_profile_edit() ) {
//exlude fields, separated by comma
//$retval['exclude_fields'] = '1';
//exlude groups, separated by comma
$retval['exclude_groups'] = '3';
}
return $retval;
endif;
}
add_filter( 'bp_after_has_profile_parse_args', 'bpfr_hide_profile_field_group' );
Change History (8)
#3
@
9 years ago
I don't want the field group tab to appear on edit screen.
I have no custom edit template.
This snippet worked with 2.1. I don't remember if it hide only the content of a field group or the whole group tab, including content. But it seems logic that if i remove a group from a conditionnal context, it shouldn't appear.
function bpfr_hide_profile_field_group( $retval ) {
if ( bp_is_active( 'xprofile' ) ) :
// hide profile group/field to all except admin
if ( !is_super_admin() ) {
//exlude fields, separated by comma
$retval['exclude_fields'] = '1';
//exlude groups, separated by comma
$retval['exclude_groups'] = '1';
}
return $retval;
endif;
}
add_filter( 'bp_after_has_profile_parse_args', 'bpfr_hide_profile_field_group' );
I remember vaguely that it was also a time where it was not possible to hide groups via parse_args. It came in later and i could use such snippet. Sorry, have no more details for this.
#5
@
9 years ago
Hi @danbp: I was testing your snippet, too, and it works to hide groups from the "view" screen of the profile (if you remove the bp_is_user_profile_edit() conditional), but has no effect on the edit view.
As @boonebgorges says above, to prevent the profile group "tab" link from being created, the logic chain starts with bp_profile_group_tabs() in members/edit.php which leads me to bp_get_profile_group_tabs(), where the groups are fetched via bp_profile_get_field_groups(). I can exclude tabs using the following code:
function bpfr_hide_profile_field_group( $groups ) {
// Hide the profile group tabs on the edit interface for all non-admins.
if ( bp_is_user_profile_edit() && ! current_user_can( 'bp_moderate' ) ) {
$remove_groups = array( 2 ); // Put the IDs of the groups to remove here, separated by commas.
foreach ( $groups as $key => $group_obj ) {
if ( in_array( $group_obj->id, $remove_groups ) ) {
unset( $groups[$key] );
}
}
}
return $groups;
}
add_filter( 'bp_profile_get_field_groups', 'bpfr_hide_profile_field_group' );
Note that this won't prevent users from actually editing those groups--they can still access the edit group url directly, and the default template will allow access.
Your code *does* work for excluding fields on the edit form, though. So maybe that's a source of confusion.
#6
@
9 years ago
Thanks @dcavins,
using your snippet throws 2 notices, but remove the tab.
- Undefined offset: 1 in...bp-xprofile\bp-xprofile-template.php on line 1033
- Trying to get property of non-object in bp-xprofile\bp-xprofile-template.php on line 1033
Problem is that the i test with 3 groups. All groups have some content.
id 1 Base
id 2 ABC
id 3 XYZ
I don't want id 2 to show - your snippet as is.
When i'm on the edit screen, i got the notices.
Changing the id to 3. No tab 3 (ok), no notices (ok)
And finally, change to hide tab id 1.
Again, notices on edit screen (the first with Undefined offset: 0 ), tab 1 isn't showing but his content appears under tab 2 and i have no tab 3.
To resume, the only tab correctly hidden when using multiple group tabs is the last one.
#7
@
9 years ago
Oh, we're using a for() loop with an incrementor rather than some other loop (like a foreach), so the array's key values matter. This should avoid the problem:
function bpfr_hide_profile_field_group( $groups ) {
// Hide the profile group tabs on the edit interface for all non-admins.
if ( bp_is_user_profile_edit() && ! current_user_can( 'bp_moderate' ) ) {
$remove_groups = array( 2 ); // Put the IDs of the groups to remove here, separated by commas.
foreach ( $groups as $key => $group_obj ) {
if ( in_array( $group_obj->id, $remove_groups ) ) {
unset( $groups[$key] );
}
}
$groups = array_values( $groups );
}
return $groups;
}
add_filter( 'bp_profile_get_field_groups', 'bpfr_hide_profile_field_group' );
or, to save a foreach, this is probably better:
function bpfr_hide_profile_field_group( $groups ) {
// Hide the profile group tabs on the edit interface for all non-admins.
if ( bp_is_user_profile_edit() && ! current_user_can( 'bp_moderate' ) ) {
$remove_groups = array( 2 ); // Put the IDs of the groups to remove here, separated by commas.
$new_set = array();
foreach ( $groups as $key => $group_obj ) {
if ( ! in_array( $group_obj->id, $remove_groups ) ) {
$new_set[] = $group_obj;
}
}
return $new_set;
}
return $groups;
}
Sorry for the earlier mistake!
#8
@
9 years ago
No worry! Your second option is the good one @dcavins !
It solves the question: how to hide/remove a field group tab from front-end xprofile edit screen.
That said, sorry if it sounds picky, this should be disambiguated on the codex.
I'm thinking about the use of $retval['exclude_groups'] = '3'; and
bp_after_has_profile_parse_args filter.
While using this filter, you can only exclude a field group from the public profile view.
If you want this to happen on the edit screen - which is, IMHO, the place where you essentially might use such a scenario - you need to use the bp_profile_get_field_groups filter.
If i understood this ticket correctly, i think it should be mentionned/explained for "Profile" templating with bp_parse_args, as it is slightly different from ie. "Activities" , who hasn't an edit screen.
Not really different, but, let's say "incomplete", for this special case of "Profile".
What do you think ?
Hi @danbp - Thanks for the ticket.
When you say "hide a profile field group from the profile edit screen", I assume you mean that you don't want the profile "tab" to appear, correct?
The code you've provided doesn't do this in 2.7 or in 2.1/2.0. The tabs are not generated by
bp_has_profile()but bybp_profile_get_field_groups(), so yourbp_after_has_profile_parse_argswould not affect it.Can you provide more context for the code that works in 2.1 but doesn't work in 2.7? Do you have a custom edit.php template that is, for example, showing all field groups on a single tab?