Last active
July 18, 2023 15:56
-
-
Save ipokkel/482f8ed32f6674a5c74c2f2f0c8dd84a to your computer and use it in GitHub Desktop.
Removes the First Name, Last Name, and Display Name fields on the Member Profile Edit page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This recipe removes the First Name, Last Name, and Display Name fields on the Member Profile Edit page. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_member_profile_edit_unset_user_object_fields( $user_fields ) { | |
unset( $user_fields['first_name'] ); | |
unset( $user_fields['last_name'] ); | |
unset( $user_fields['display_name'] ); | |
// unset( $user_fields['user_email'] ); // uncomment to also remove the email address | |
return $user_fields; | |
} | |
add_filter( 'pmpro_member_profile_edit_user_object_fields', 'my_pmpro_member_profile_edit_unset_user_object_fields' ); | |
function my_pmpro_user_profile_update_errors_skip_errors( &$errors, $update = null, &$user = null ) { | |
// Let's check which user fields are set. | |
$user_fields = apply_filters( | |
'pmpro_member_profile_edit_user_object_fields', | |
array( | |
'first_name' => __( 'First Name', 'paid-memberships-pro' ), | |
'last_name' => __( 'Last Name', 'paid-memberships-pro' ), | |
'display_name' => __( 'Display name publicly as', 'paid-memberships-pro' ), | |
'user_email' => __( 'Email', 'paid-memberships-pro' ), | |
) | |
); | |
// array of default error messages that we might need to check for. | |
$error_messages = array( | |
'display_name' => __( 'Please enter a display name.', 'paid-memberships-pro' ), | |
'user_email' => __( 'Please enter an email address.', 'paid-memberships-pro' ), | |
); | |
// Only keep the error messages for the user fields that are set. | |
foreach ( $user_fields as $key => $field ) { | |
if ( array_key_exists( $key, $user_fields ) ) { | |
unset( $error_messages[ $key ] ); | |
} | |
} | |
// Remove the error messages from the errors array. | |
foreach ( $errors as $key => $error ) { | |
if ( in_array( $error, $error_messages ) ) { | |
unset( $errors[ $key ] ); | |
} | |
} | |
} | |
add_action( 'pmpro_user_profile_update_errors', 'my_pmpro_user_profile_update_errors_skip_errors', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment