Skip to content

#46744 Prevent the default role from being set to a privileged role when user registration is open - #12977

Open
johnbillion wants to merge 5 commits into
WordPress:trunkfrom
johnbillion:46744-default-role
Open

#46744 Prevent the default role from being set to a privileged role when user registration is open#12977
johnbillion wants to merge 5 commits into
WordPress:trunkfrom
johnbillion:46744-default-role

Conversation

@johnbillion

@johnbillion johnbillion commented Aug 11, 2026

Copy link
Copy Markdown
Member

This change prevents the default role from containing a dangerous value when user registration is open. It does this via filtering the default_role option value, meaning a privileged value in the option in the database gets overridden.

The Editor and Administrator roles are excluded by default (when user registration is open). The default_role_excluded_roles filter is introduced to facilitate adding to or removing from the list of excluded roles.

On a Multisite installation, the users_can_register_signup_filter() filter is applied to option_users_can_register. This PR doesn't need to handle that specifically, but tests have been added to ensure it's covered.

Trac ticket: Core-46744

Use of AI Tools

Changes written manually, tests written by Opus 5.

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props johnbillion, irozum, audrasjb.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

irozum

This comment was marked as low quality.

@audrasjb audrasjb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic looks good to me.
I'm quite fine with the filter_default_role function name as long as it is not used at all (at least no occurrence exists in the wpdirectory.net database yet), but we definitely need to add a docblock before the function, event if it basically just show the same content used below in the hook.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens WordPress user registration by preventing the default_role option from effectively resolving to a privileged role when user registration is enabled, while also providing a new filter (default_role_excluded_roles) to customize which roles are disallowed.

Changes:

  • Adds an option_default_role filter to override privileged default roles to subscriber when registration is open.
  • Introduces the default_role_excluded_roles filter (defaulting to administrator and editor) and wires it into the General Settings role dropdown behavior.
  • Adds PHPUnit coverage for both direct filtering behavior and end-to-end user creation role assignment.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/phpunit/tests/functions/filterDefaultRole.php Adds unit/integration tests covering filtering behavior and user creation role assignment.
src/wp-includes/functions.php Introduces the filter_default_role() option filter callback and the default_role_excluded_roles filter.
src/wp-includes/default-filters.php Hooks the new default-role filter callback to option_default_role.
src/wp-admin/options-general.php Uses default_role_excluded_roles to seed the excluded roles used in the default role dropdown logic.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/wp-admin/options-general.php
* @param string $default_role The default role for new user registrations.
* @return string The filtered default role for new user registrations.
*/
function filter_default_role( $default_role ) {
Comment thread src/wp-includes/functions.php Outdated
@johnbillion
johnbillion requested a review from audrasjb August 17, 2026 01:17

@audrasjb audrasjb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me 👍

@johnbillion
johnbillion requested a review from jeremyfelt August 28, 2026 16:40

@jeremyfelt jeremyfelt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

* @return string The filtered default role for new user registrations.
*/
function filter_default_role( $default_role ) {
static $filtering = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Is there a static variable in use here? It feels unnecessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants