How To Disable Nickname (or any name) Changes In WordPress & BuddyPress

I recently started using BuddyPress, well BuddyBoss actually to be precise & I noticed that oddly, there is no way to disable users from changing their names or nicknames (display names).

I mean I get it… People like to have the freedom to change their nicknames on websites & many social media sites do indeed allow it…

But in my opinion, it could be a complete nightmare from an admin & user experience point of view – especially since WordPress by default allows unlimited name changes.

Anyway, I figured there should be at least the option… And there wasn’t, so instead I had to create something for myself.

Should be easy enough, right?

Wrong.

I figured that there are 2 methods I could go down. I could change the whole BuddyBoss/BuddyPress platform to display the user’s username rather than their nickname (which by default can’t be changed), or I could simply restrict changes to the nickname.

It seemed like overkill going down the route of switching everything to the username and it could also be a potential nightmare in the future in terms of running updates, etc.

So I decided to go down the route of disabling nickname edits instead.

Oddly, pretty much every solution that I found online only targets the form field itself, which means that it could be VERY easily bypassed by any users with some slight tech know-how.

Or, it could also be bypassed by somebody who had simply disabled their Javascript. Not good.

Now, my solution is by no means perfect, and it’s very hacky indeed, but it works – and not only does it take a client-side approach at disabling changes, but it takes a serverside approach too in order to prevent it being bypassed.

Here’s The Code…

These code snippets should be added to the functions.php file in your themes folder. I’ll explain each snippet individually as you’ll likely need to customize them to your needs, and you may not even need to include the last one (depending on your setup).

Firstly…


// Hacky frontend function to disable nickname changes (for non admins)
if (!is_super_admin()) {
function disable_nickname_changes() {
if ((strpos($_SERVER['REQUEST_URI'], '/members/') !== false) && (strpos($_SERVER['REQUEST_URI'], '/edit/group/1') !== false)) {
    ?>
        <script>
            jQuery( document ).ready(function() {
                jQuery('#field_3').prop('disabled', 'disabled');
				var el = document.getElementById("profile-group-edit-submit");
				el.style.display = 'none';
            });
        </script>
    <?php
    }
}
add_action( 'wp_head', 'disable_nickname_changes', 15 );
}

This bit of code simply detects the URL that the user is viewing. If the URL contains “/members/” and “/edit/group/1” then it will display the jQuery snippet on the page.

You will obviously need to edit the URL match query depending on the URL of the page where your nickname field is located.

Then, as you can see from the jQuery code, it disables the field with the id of “field_3” (which was my nickname input field) & I also set it to hide the submit button (“profile-group-edit-submit”) because the nickname input was the only field on that page so there was simply no need for it to be there.

Great. That works… But if a user has Javascript disabled, they’ll still be able to change their nickname.

We need a server-side solution, so I added this snippet:


// Function to prevent nickname changes being saved in database (for non admins)
if (!is_super_admin()) {
function disable_nickname_changes_serverside( $data ) {
if ( 3 == $data->field_id )
$data->field_id = false;
return $data;
}
add_action( 'xprofile_data_before_save', 'disable_nickname_changes_serverside' );
}

Pretty self-explanatory… It removes the field ID for field 3, which was the ID of my nickname field so when the form is submitted, it just fails because the nickname field is essentially missing.

For most sites, this will be enough for you to disable nickname changes, however for those running BuddyBoss or BuddyPress you might need to go one step further.

That’s because the main member edit page is located at “/members/useraccount/profile/edit/”, however, the user can also still go via “/profile.php” to edit their data.

To prevent that, I added a redirect from those pages for non-adminstrators, as shown below:


// Function to redirect normal users away from default profile pages
if (!is_super_admin()) {
function redirect_default_profile_pages() {
header("Location: /");
die();
}
add_action( 'admin_head', 'redirect_default_profile_pages', 15 );
}

Good enough for me!

After all, it’s not the end of the world if a nickname gets changed… But I’d just prefer for them not to be changed.

Additional Notes

The snippets I’ve added above disallow nickname changes for everybody except the administrators. If necessary, just change the following line to suit your needs if you want to allow or disallow people based on permissions


if (!is_super_admin()) {

If you want to change the code to disable other fields rather than the nickname, such as the display name or first name or last name, etc, that’s easy enough to do too.

All you need to do is view the source of your profile page & find the ID of the fields you wish to disable. In the example above the field, I disabled was field 3, so simply switch the number 3 out for the ID of the fields you wish to target.

And if you have multiple fields on the profile page, you may wish to let the submit button stay on the page… So just edit the jQuery code accordingly.

But if you have any questions, just ask. I found it pretty frustrating so I’d be more than happy to help to save you the hassle.

17 thoughts on “How To Disable Nickname (or any name) Changes In WordPress & BuddyPress”

  1. Hello Darlay,
    we are using Buddyboss and Paid Membership Pro. I have two questions you might be able to help with.

    1. Is there anyway to allow the Nick Name when signing up to ad Capital Letters and Spaces? We have a business membership website and we want that field to be our members business name?

    2. Is there a way to redirect the user to the Dashboard after a successful payment when using Paid Membership Pro?

    Thank you and looking forward to hearing from you.

    Reply
    • Ok may have found out the redirect. I just configured “Login and Logout” plugin and I’m going to try it. Ill update when done.

      Oh and I understand the use of the Nickname/Username now and just added a description to the registration form so users will understand why they can’t use uppercase or spaces.

      Reply
      • Did you manage to figure it out? 🙂

        You should be able to use capital letters in nicknames in BuddyBoss, but just be careful if you run any of the “cleanup” functions that come bundled in with it as there’s one particular function that turns everything to lowercase… Ugh. BuddyBoss is “almost” a great platform, but they really did overlook some vital components.

        Also, your site (the one you linked in your comment) is slowwwwwwwwwwwwww man. You need to fix that as a priority.

        Reply
  2. I have a similar question.
    I want to allow the user to choose which displays:
    First Name
    First + Last
    Nickname
    Company Name
    On a business directory website, the default options (1) don’t allow for company name display and (2) allow the user to choose which displays. I want employers to be able to display their Company Name and job-seekers to be able to display their choice of the other options. Is this do-able?

    Reply
  3. Thank you! I have three things I’m trying to do. BB support says “find a developer”.

    #1. I want to have more options for the display name, in particular, I want to allow a user when editing their profile to choose from what is there out of the box, nickname, First Name or First Name + Last Name but also Company Name. Crucial to be able to let them select Company Name (custom field) and have it display as their main display name so it will work for filtering, searching, sorting etc.

    #2. I want to be able to display a particular profile on a particular page so I can control SEO and the look and feel of the page better. For example, with CozmosLabs Profile Builder Pro, you can use a shortcode like
    [wppb-list-users single name=”userlisting” id=”3″]
    From this page
    https://www.cozmoslabs.com/docs/profile-builder-2/modules/user-listing/
    But…Profile Builder Pro lacks much of the functionality of BB and I’ve just paid for the BB app option so I want to stick with BB. Also, PBP says they integrate with BB but it looks complicated and I’m not sure how that would work out with the app aspect.
    So, I really want to be able to use the same kind of shortcode and build company profiles for say 40 companies. I want to be able to select a company by id and display their profile on a particular page.

    #3. Then, I want to offer to the companies the option to purchase an expanded version of their profile. So I want to be able to use conditional logic on fields to say “IF the viewer has role xyz AND the profile id has whatever…role? Custom field checkbox? THEN show this field.

    I’ve installed the BuddyDev conditional logic plugin but it doesn’t quite to it. It’s so close. It lets you hide/show based on user input fields but not the criteria I said above. I talked to BuddyDev support and they said talk to BB. There has to be a not that hard way to say “Show this field IF viewer has role(s) x, y and/or z AND the profile ID has custom field a, b, or c checked or profile ID user role is a, b or c.

    I see this code example which looks like it does exactly what I want, but other than knowing how to paste into my Code Snippets, I’m not sure what to change/tweak to accomplish the above.

    https://www.buddyboss.com/resources/reference/functions/bp_xprofile_get_hidden_fields_for_user/

    http://hookr.io/filters/bp_xprofile_get_hidden_fields_for_user/

    https://hotexamples.com/examples/-/-/bp_xprofile_get_hidden_fields_for_user/php-bp_xprofile_get_hidden_fields_for_user-function-examples.html

    I’d sure appreciate help nailing down those three things. They are what is holding up my project way behind on the deadline 🙂

    Reply
  4. I have an idea I’m implementing.
    Bought Custom Profile Tabs Pro from Buddy Dev.
    Added a new tab for a page I can control for enhanced details.
    I can add a form view from Formidable Forms for basic or enhanced depending on if they have paid.

    It’s much more manual than I was hoping but I guess it works.
    I renamed the First Name field to First Name or Company Name
    This is definitely not perfect but at least now the company name displays with First Name selected as the display name option in BuddyBoss.

    I’d still like to understand the possibilities for 1-3 above.

    Reply
    • Sounds like you really need a developer to help you with that. What you’re trying to do is pretty bespoke. I’m all for helping, but that’s way above “help”… What you’re asking for is literally an entire project.

      Reply
  5. Got it.
    Is there any chance that just the display company name if the company name custom field is populated is not a big project?

    Reply
  6. Oh yay! Thank you!
    Can you flesh out the {do whatever}? I am very much not a coder.
    Super grateful to you!

    Can the ($company_name != “”) be empty in the ” “? In other words, is that saying “whatever is in the company_name field, if there is anything, display that, else display whatever else? But not sure how to write the whatever else.
    Let’s say we want the whatever else to be First Name, Last Name
    Or if we want it to be nickname
    Can you show me what those two would look like?

    Reply
    • Bro, if you’re very much not a coder why are you trying to do all this yourself & fighting so hard against hiring a developer? 🤔 I’m afraid I’m currently unavailable for custom dev work.

      Reply
  7. I’m not fighting against it. I’m also just trying to learn what I can and do as much of it as I can. I thought if it’s a simple snippet to let me have the company name display I could drop it into my snippets. I was hoping it was that easy.

    Reply

Leave a Comment