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 );
}
@joost-abrahams
Copy link

I did notice one difference as before using this code: the "usename" is now used as the "Display name publicly as" not the "first name" and "last name"

@christianwach
Copy link
Author

@joost-abrahams I'm interested in better understanding what you mean. Can you explain in a bit more detail?

@christianwach
Copy link
Author

@joost-abrahams Ah, I think this what you mean...

If you have First Name and Last Name fields in BuddyPress 8+ (and both are configured as WordPress Text Field and mapped to the correct WordPress User fields) and you use this plugin to remove the default Name field from the Registration Form, then the WordPress User Display Name field is not set to "FirstName LastName" but is set to the WordPress username instead.

My BP xProfile WordPress User Sync plugin used to handle that process in full but (since BuddyPress 8) I've retired it in favour of built-in BuddyPress functionality. The two tickets you could look at to understand what's going on are xprofile field support for WordPress Profile Fields and The xProfile "Name" Field. The bottom line is that work on the Name field stalled, so you will have to roll your own solution.

I've uploaded a Gist which enforces the format of the BuddyPress Name field and WordPress Display Name field to be "FirstName LastName" although it does not account for all situations e.g. the Name field still appears on the BuddyPress Profile Edit screen.

I may create a plugin that's a replacement for BP xProfile WordPress User Sync that enforces "FirstName LastName" everywhere in BuddyPress and WordPress - but it's a fair bit of work so don't hold your breath :)

@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