Add Snippet To Project
Are you seeking more control over the default profile fields in your Wordpress site? Perhaps there are certain details you don’t need users to fill in or information to remain private. Luckily, WordPress provides ways to remove such fields from user profiles. This article guides you through an effective, non-technical approach to remove those default profile fields from your WordPress site.
function wpturbo_remove_default_profile_fields( $contactmethods ) {
unset( $contactmethods['aim'] );
unset( $contactmethods['jabber'] );
unset( $contactmethods['yim'] );
return $contactmethods;
}
add_filter( 'user_contactmethods', 'wpturbo_remove_default_profile_fields', 10, 1 );
In the provided code snippet, we define a function called wpturbo_remove_default_profile_fields()
that takes one parameter $contactmethods
. This parameter represents the default set of contact fields that are displayed on a user’s WordPress profile page.
function wpturbo_remove_default_profile_fields( $contactmethods ) {
...
}
Inside this function, we have three unset()
function calls where each one removes one of the default contact fields: ‘aim’, ‘jabber’, and ‘yim’. Here, unset()
is a PHP function that destroys the specified variables. In our case, it’s removing these methods from the contact methods array.
unset( $contactmethods['aim'] );
unset( $contactmethods['jabber'] );
unset( $contactmethods['yim'] );
You can specify any other contact methods you wish to remove in place of ‘aim’, ‘jabber’, and ‘yim’. After all required fields have been removed, the function returns the modified $contactmethods
array.
return $contactmethods;
To apply this function and its changes to the profile fields, we need to attach it to the ‘user_contactmethods’ filter hook. The add_filter()
function does this.
add_filter( 'user_contactmethods', 'wpturbo_remove_default_profile_fields', 10, 1 );
In the add_filter()
function, ‘user_contactmethods’ is the filter hook that WordPress provides for manipulating the contact methods fields. ‘wpturbo_remove_default_profile_fields’ is the name of the function we defined to modify these fields.
The third parameter ’10’ describes the priority at which our function executes. Lower numbers correspond with earlier execution, and functions with the same priority execute in the order in which they were added to the filter.
The last parameter ‘1’ indicates the number of arguments our function accepts. In this case, our function accepts only one argument, which is $contactmethods
.