Skip to content

Instantly share code, notes, and snippets.

@christianwach
Last active April 5, 2024 20:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianwach/06d878edc277ce1ca8a7e59a82052744 to your computer and use it in GitHub Desktop.
Save christianwach/06d878edc277ce1ca8a7e59a82052744 to your computer and use it in GitHub Desktop.
Hides the default Name field on BuddyPress Registration form and Profile Edit screens.
<?php
/**
* Plugin Name: BP Hide Name Field
* Description: Hides the default Name field on BuddyPress Registration form and Profile Edit screens.
* Plugin URI: https://gist.github.com/christianwach/06d878edc277ce1ca8a7e59a82052744
* Version: 1.1
* Author: Christian Wach
* Author URI: https://github.com/christianwach
*/
add_filter( 'bp_after_has_profile_parse_args', 'bphnf_intercept_profile_query_args' );
/**
* Filters the xProfile query to hide the "Name" field on the Registration screen.
*
* Apply to registration form whichever page it is displayed on, whilst avoiding
* splitting the Name field into First Name and Last Name fields in the profile
* display loop of the User. Note that we cannot determine if we are in the loop
* prior to the query, so we test for an empty User ID instead.
*
* @since 1.0
*
* @param array $args The existing arguments used to query for fields.
* @return array $args The modified arguments used to query for fields.
*/
function bphnf_intercept_profile_query_args( $args ) {
// User must be logged out.
if ( is_user_logged_in() ) {
return $args;
}
if ( ! bp_is_user_profile() OR ( bp_is_user_profile() AND empty( $args['user_id'] ) ) ) {
// Query only group 1.
$args['profile_group_id'] = 1;
// Exclude name field (bp_xprofile_fullname_field_id is available since BP 2.0).
$exclude_fields = bp_xprofile_fullname_field_id();
// Merge with existing if populated.
$args['exclude_fields'] = bphnf_merge_excluded_fields( $args['exclude_fields'], $exclude_fields );
}
return $args;
}
add_filter( 'bp_after_has_profile_parse_args', 'bphnf_intercept_profile_query_filter' );
/**
* Filters the xProfile query to remove the "Name" field from the Profile Edit screen.
*
* @since 1.0
*
* @param array $args The existing arguments used to query for fields.
* @return array $args The modified arguments used to query for fields.
*/
function bphnf_intercept_profile_query_filter( $args ) {
// If on profile edit screen.
if ( bp_is_user_profile_edit() ) {
// Exclude name field.
$exclude_fields = bp_xprofile_fullname_field_id();
// Merge with existing if populated.
$args['exclude_fields'] = bphnf_merge_excluded_fields( $args['exclude_fields'], $exclude_fields );
}
return $args;
}
add_filter( 'bp_xprofile_get_groups', 'bphnf_intercept_profile_fields_query', 100, 2 );
/**
* Filter the xProfile query to exclude the BuddyPress "Name" field from display.
*
* This callback excludes the default BuddyPress "Name" field on all admin screens
* where Profile Fields can be managed.
*
* @since 1.1
*
* @param array $groups The array of xProfile groups.
* @param array $args The array of arguments.
* @return array $groups The modified array of xProfile groups.
*/
function bphnf_intercept_profile_fields_query( $groups, $args ) {
// Bail if not in admin.
if ( ! is_admin() ) {
return $groups;
}
// Exclude name field.
$args['exclude_fields'] = bp_xprofile_fullname_field_id();
// Re-query the groups.
$groups = BP_XProfile_Group::get( $args );
// --<
return $groups;
}
/**
* Merge possibly comma-delimited lists of excluded fields.
*
* @since 1.0
*
* @param string $excluded_fields Comma-delimited list of fields already excluded.
* @param string $exclude_fields Comma-delimited list of fields requiring exclusion.
* @return string $excluded_fields Comma-delimited list of all fields to be excluded.
*/
function bphnf_merge_excluded_fields( $excluded_fields, $exclude_fields ) {
// If params are not arrays already, convert them.
if ( ! is_array( $excluded_fields ) ) {
if ( ! empty( $excluded_fields ) ) {
$excluded_fields = explode( ',', $excluded_fields );
} else {
$excluded_fields = [];
}
}
if ( ! is_array( $exclude_fields ) ) {
if ( ! empty( $exclude_fields ) ) {
$exclude_fields = explode( ',', $exclude_fields );
} else {
$exclude_fields = [];
}
}
// Merge with existing.
$excluded_fields = array_unique( array_merge( $excluded_fields, $exclude_fields ) );
return implode( ',', $excluded_fields );
}
@christianwach
Copy link
Author

@joost-abrahams I've updated this Gist to hide the Name field on both the Registration form and Profile Edit screens. If you don't want to hide it in certain locations, then you can comment out the appropriate add_filter() lines.

@joost-abrahams
Copy link

Thanks @christianwach.
It is working perfect now.
The updated on this gist is very welcome also.

Next time i will make screenshots to make it more easy to understand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment