Skip to content

Fix 9296: Enable custom Settings API fields in Permalinks page #9199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

vidushigupta0607
Copy link

Problem

The WordPress Permalinks settings page (options-permalink.php) was not processing custom settings that were registered through the WordPress Settings API. While developers could add custom settings to the page using register_setting() with the 'permalink' group, these settings would not persist when the form was submitted.

Root Cause

The root cause was that options-permalink.php lacked the necessary code to process and save custom settings registered through the Settings API. While WordPress provides the infrastructure to register settings with the 'permalink' group, the permalinks page wasn't implementing the logic to handle these settings during form submission.

Solution

Added code to options-permalink.php to properly handle custom settings:

// Handle custom settings registered through the Settings API.
$registered_settings = get_registered_settings();

foreach ( $registered_settings as $option_name => $option_args ) {
    if ( isset( $option_args['group'] ) && 'permalink' === $option_args['group'] ) {
        if ( isset( $_POST[ $option_name ] ) ) {
            $value = wp_unslash( $_POST[ $option_name ] );
            if ( isset( $option_args['sanitize_callback'] ) && is_callable( $option_args['sanitize_callback'] ) ) {
                $value = call_user_func( $option_args['sanitize_callback'], $value );
            }
            update_option( $option_name, $value );
        }
    }
}

This addition:

  • Retrieves all registered settings
  • Processes settings specifically registered to the 'permalink' group
  • Properly sanitizes values using registered callbacks
  • Saves the settings to the database

Example Usage

For developers looking to add custom permalink settings, here's how to properly register a setting:

register_setting(
    'permalink',           // settings group
    'my_custom_setting',   // option name
    array(
        'type' => 'string',
        'sanitize_callback' => 'sanitize_text_field',
        'default' => ''
    )
);

Impact

This enhancement enables developers to properly extend the Permalinks settings page using the WordPress Settings API, improving the platform's extensibility while maintaining consistency with WordPress coding standards and best practices.

Trac Link

## Problem
The WordPress Permalinks settings page (`options-permalink.php`) was not properly saving custom settings that were registered through the WordPress Settings API. While developers could add custom settings to the page using `register_setting()` with the 'permalink' group, these settings would not persist when the form was submitted.

## Root Cause
The issue stemmed from a mismatch in the settings group parameter check in `options-permalink.php`. The code was checking for `option_args['option_group']`, but WordPress's `register_setting()` function stores this information in `option_args['group']`. This caused the custom settings processing loop to skip over valid settings, resulting in custom fields not being saved.

## Solution
The fix involves a simple but crucial change in `options-permalink.php`:
- Changed the condition from checking `option_args['option_group']` to `option_args['group']`
- Removed debug logging statements that were used during investigation

### Testing
The fix was verified by:
1. Creating a test plugin that registers a custom permalink setting
2. Confirming the setting appears on the Permalinks page
3. Verifying the setting successfully saves to the database
4. Checking that the saved value persists across page reloads

### Example Usage
For developers looking to add custom permalink settings, here's how to properly register a setting:

```php
register_setting(
    'permalink',           // settings group
    'my_custom_setting',   // option name
    array(
        'type' => 'string',
        'sanitize_callback' => 'sanitize_text_field',
        'default' => ''
    )
);
```

## Impact
This fix enables developers to properly extend the Permalinks settings page using the WordPress Settings API, improving the platform's extensibility while maintaining consistency with WordPress coding standards and best practices.

## Trac Link
- https://core.trac.wordpress.org/ticket/9296
Copy link

github-actions bot commented Jul 4, 2025

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 vidugupta.

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

Copy link

github-actions bot commented Jul 4, 2025

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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

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