Skip to content

Options, Meta APIs: Add a validate_callback argument to register_setting() - #16384

Draft
jorgefilipecosta wants to merge 3 commits into
WordPress:trunkfrom
jorgefilipecosta:add/register-setting-validate-callback
Draft

jorgefilipecosta wants to merge 3 commits into
WordPress:trunkfrom
jorgefilipecosta:add/register-setting-validate-callback

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #10892.

Currently, core/settings-update validates the values of seven core settings in a private method of the ability, and plugins have no way to add similar rules to their own settings.

This PR implements the following changes:

  • register_setting() accepts a validate_callback argument, which receives the value and the option name and returns true or a WP_Error.
  • core/settings-update calls it before saving anything, and the core rules move to callbacks registered in register_initial_settings().
  • A setting registered again without a validate_callback keeps its callback.
  • Shape change: the page settings only accept pages in a status the Settings screens offer.

Trac ticket: https://core.trac.wordpress.org/ticket/66900

Testing

Verify unit tests are passing:

vendor/bin/phpunit --group abilities-api,option,restapi

AI disclosure: the change and tests were drafted with AI assistance (Claude Code).

@github-actions

github-actions Bot commented Dec 2, 2026

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.

…etting()`.

A setting can be registered with a callback that checks whether it can use a value. The callback receives the value and the option name, and returns true or a `WP_Error`. It is kept with the registered setting and listed by `get_registered_settings()`; saving the option with `update_option()` does not call it.

The `core/settings-update` ability calls the callback of each named setting before the first write, so a refused call still changes nothing, and returns the same `settings_invalid_value` error. The value rules of the timezone, language, default post format, default category and page settings move from the ability into callbacks registered in `register_initial_settings()`, so plugins can add rules for their own settings the same way.

The front page and the page for posts now accept 0 or a published page, and the privacy policy page 0 or a published or draft page, the statuses the Settings screens offer for them.

See #64616.
…d again.

Registering a setting again without a `validate_callback` now keeps the callback it was registered with, so a plugin that registers a core setting again keeps the rule of Core.

The language error no longer names the option.

See #64616.
@jorgefilipecosta
jorgefilipecosta force-pushed the add/register-setting-validate-callback branch from d66de43 to 49f5d0c Compare December 2, 2026 16:18
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.

1 participant