Paid Memberships Pro has a number of ways that you can control the allowed format and structure of usernames for new members. In this recipe, we show you how to restrict certain words or strings in usernames.

This tutorial is especially useful if your membership site shows usernames in frontend features. For example, if your site has a community forum, directory, or connects to a third-party platform like Discord, you can use this code to preserve the quality of your membership by blocking unwanted terms in their displayed names.

Banner Image for Restrict Certain Words in Usernames Code Recipe

About the Code Recipe

This recipe will allow you to set a list of terms that you do not want people to use in their membership account’s username.

When a username is submitted with one or more restricted words, checkout is blocked and a customizable error message is shown.

Screenshot of invalid username due to restricted word or words

The Code Recipe (PHP)

Video: Coding with Sam Walkthrough

Adding the Recipe to Your Website

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.

Update line 44 to include the terms you want to block, separated by a vertical bar character. You can also customize line 29 to change the displayed error message if the term is used.

Free Course: Membership Site Development—The Basics

Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.

Featured Image for Membership Site Development Course: The Basics
Was this article helpful?
YesNo