Cap Display Name and Username Length in WordPress — Without Touching the Database

Date:

WordPress lets a display name run to 250 characters, and usernames can be unwieldy too. Left unchecked, they overflow author boxes, comment threads, and menus on mobile. You can cap both — cleanly, reversibly, and without editing the database — with a couple of small snippets.

The short answer: To limit user-field length in WordPress, prefer code over a database edit. Cap the display name by truncating it on the pre_user_display_name filter; cap the username at registration by adding an error on the registration_errors filter (reject, don’t silently truncate — a username is an identifier). Both go in a snippet (functions.php, a site plugin, or a code-snippets plugin), are easy to change, and are far safer than altering the wp_users column directly.

By default WordPress allows a 250-character display name, which is generous to the point of breaking layouts — a stretched author box, overflowing comments, awkward wrapping on phones. Usernames have their own untidiness. Two small snippets fix both without the risk of a schema change.

Two ways to cap user-field length — code winsReversible snippet vs a risky schema edit
Database edit

Shorten the wp_users column in phpMyAdmin. A wrong edit can break the site. Global only. Changing the limit = editing the schema again. Back up first.

fast but risky
Code snippet

Filters cap the display name + validate username length. Low risk, reversible, retune by one number, per-role possible, version-controlled.

Why cap these fields

An over-long display name or username isn’t a security problem — it’s a presentation and tidiness one:

  • Broken layouts — a 200-character display name stretches author boxes and profile cards.
  • Overflowing comments and menus — long names wrap awkwardly or push other elements around.
  • Worse on mobile — narrow viewports amplify every overflow.

Capping at something reasonable (say 50–70 characters for a display name) lets users personalize while keeping the design intact.

Cap the display name (truncate on a filter)

The display name is shown, not used as an identifier, so truncating it is fine. Hook pre_user_display_name:

// Cap display name length. Add to functions.php or a code-snippets plugin.
add_filter( 'pre_user_display_name', function ( $display_name ) {
    $max = 50; // your limit
    return ( strlen( $display_name ) > $max ) ? substr( $display_name, 0, $max ) : $display_name;
} );
Code language: PHP (php)

New or updated display names longer than the limit are trimmed automatically. Change the cap by editing one number — no database trip.

Cap the username (reject at registration)

A username is an identifier, so don’t silently truncate it — that could collide with another user or confuse login. Instead, reject an over-long username at registration with a clear error, on registration_errors:

// Reject usernames longer than the limit at registration.
add_filter( 'registration_errors', function ( $errors, $sanitized_user_login ) {
    $max = 30; // your limit
    if ( strlen( $sanitized_user_login ) > $max ) {
        $errors->add( 'username_too_long', "Username must be $max characters or fewer." );
    }
    return $errors;
}, 10, 2 );
Code language: PHP (php)

This tells the user what’s wrong instead of quietly altering their chosen name.

Code vs the database edit

You can shorten the display_name column in wp_users via phpMyAdmin, but weigh it against the snippet:

  • Database edit — fast, but risky (a wrong change can break the site), applies globally, and changing the limit later means editing the schema again. Back up first.
  • Code snippet — low risk, reversible, flexible (change a number, or apply different limits per role), and version-controllable. This is the recommended default — a textbook snippet-over-a-plugin case.

A username is an identifier — reject an over-long one with an error; only the display name is safe to quietly trim.

Both approaches work on multisite. If you only ever need one fixed limit and are comfortable in phpMyAdmin, the database edit is quicker — but for anything you might tune later, the code method wins.

Cap these with code, not a schema change: one number to edit, fully reversible, and no chance of breaking the users table.

The short version

WordPress allows a 250-character display name and loose usernames, which overflow layouts. Cap them with code rather than a database edit: truncate the display name on the pre_user_display_name filter (display only, so trimming is fine), and reject over-long usernames at registration via registration_errors (a username is an identifier, so error rather than truncate). Both live in a small snippet, are reversible, and let you change the limit by editing one number — safer and more flexible than altering the wp_users column, though the database edit is quicker for a single fixed limit.

Frequently Asked Questions

What’s the default display name length in WordPress?

The display_name column is varchar(250), so up to 250 characters — generous enough to break author boxes, comments and mobile layouts. Capping it around 50–70 characters keeps names personal without disrupting the design.

Should I limit these in the database or with code?

Code, in most cases. A snippet on WordPress’s filters is low-risk, reversible, and easy to retune, and it can apply different limits per role. Editing the wp_users column directly is faster but risky, global, and awkward to change later — and always needs a backup first.

Why reject a long username instead of truncating it?

Because a username is a login identifier. Silently truncating it could collide with an existing user or break the person’s login. Rejecting an over-long username at registration with a clear error lets them choose a valid one instead.

Will this affect existing users?

The display-name filter trims names as they’re saved, so existing long names shorten next time they’re updated (or immediately, if you edit the column). The username check only runs at registration, so existing usernames are untouched.

Does this work on WordPress Multisite?

Yes. Both the display-name filter and the username-length check work on multisite; place the snippet in a must-use plugin to apply it network-wide across all subsites.

Share post:

Subscribe

Popular

More like this
Related

How to Merge Spreadsheets by URL (and Why Our Tool Reported 5 Misses That Weren’t)

Matching two spreadsheets on a URL column is the same job as matching a stock list to a price list by product code. It goes wrong in the same two places: how you clean the code, and how you count what didn't match.

Many-to-One: Why a Mapping Backlog Outgrows Its Target Taxonomy

You're mapping a messy source vocabulary onto a clean target taxonomy. The target has about 5,600 slots, so the job must be roughly 5,600 decisions — right? The backlog is already past 8,900, and still climbing.

The Fix That Didn’t Take: When a Content-Hash Gate Skips Your Code Change

You shipped a fix to how one field is computed, re-ran the pipeline across everything, and it reported a clean run. Then the consistency check still flags those records as wrong. The fix is in the code, the rebuild "ran" — and the records never changed. Your own optimisation skipped them.

Your Dedup Scan Is Over-Counting — Because It Forgot What You Already Fixed

The report said 80 duplicate groups still to resolve. You'd been resolving them for days. You run it again after a big batch and it *still* says 80-ish. The backlog isn't stubborn — the scan is counting work you already finished.