Opened 5 months ago
Closed 2 months ago
#65027 closed enhancement (fixed)
Add ID to Section titles generated by Settings API
| Reported by: | wetah | Owned by: | afercia |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.1 |
| Component: | Administration | Version: | |
| Severity: | minor | Keywords: | has-patch has-test-info commit |
| Cc: | Focuses: | ui, accessibility, administration |
Description
I see that there's a lot of debate around improvements to the Forms generated by do_settings_sections.
- https://core.trac.wordpress.org/ticket/51086
- https://core.trac.wordpress.org/ticket/43303
- https://core.trac.wordpress.org/ticket/39441
I know more radical changes won't move up easily so I would like to suggest a simple one: having the section ID in the section title. For instance this:
<?php if ( $section['title'] ) { echo "<h2>{$section['title']}</h2>\n"; }
To become something like this:
<?php if ( $section['title'] ) { echo '<h2 id="' . $section['id'] . '">' . $section['title'] . '</h2>\n'; }
For consistency, I would add the IDs also to the Core forms that have sections not generated by do_settings_sections, such as options-media.php, options-reading.php, etc. They already do something different which is to add a 'title' class to all their section titles. This makes me think that maybe we should have a filter to change that title markup in the end... but that can be for another topic.
The main motivation for doing that is to allow hash navigation via URL. We could easily in the documentation have links such as wp-admin/options-discussion.php#avatars, which I think it is a huge benefit if we're talking long forms. I know it wouldn't solve more complex issues but can be beneficial even for Accessibility and for those who want to customize their forms via CSS, as discussed in the other linked tickets.
Attachments (1)
Change History (15)
This ticket was mentioned in PR #11472 on WordPress/wordpress-develop by @rakeshfalke.
5 months ago
#1
- Keywords has-patch added
This ticket was mentioned in Slack in #accessibility by joedolson. View the logs.
5 months ago
#4
@
3 months ago
An additional benefit this could add is having a hook that can be used for aria-describedby or aria-labelledby if a user needed those to reference the existing section heading. Seems useful to me.
This ticket was mentioned in Slack in #accessibility by joedolson. View the logs.
3 months ago
This ticket was mentioned in Slack in #accessibility by joedolson. View the logs.
2 months ago
#7
@
2 months ago
- Summary Add ID do Section titles generated by Settings API → Add ID to Section titles generated by Settings API
@afercia commented on PR #11472:
2 months ago
#8
I pushed two commits:
- Uses
wp_unique_idwith a prefix that is made of a stringwp-settings-section-+ the section ID. - For the core settings H@ headings, I just prepended
wp-settings-section-to the IDs added in this PR.
To quickly test the Settings API you can temporarily add this snippet in the options-reading.php file:
add_settings_section( 'awesome-avatars', 'My avatars section title', '__return_false', 'andreatest' ); add_settings_field( 'color_scheme', __( 'Color Scheme' ), '__return_false', 'reading', 'andreatest' ); do_settings_sections( 'andreatest' ); add_settings_section( 'nice-icons', 'My icons section title', '__return_false', 'andreatest2' ); add_settings_field( 'color_scheme', __( 'Color Scheme' ), '__return_false', 'reading', 'andreatest2' ); do_settings_sections( 'andreatest2' );
It will add two section headings. The IDs on the headings will be:
wp-settings-section-awesome-avatars-1wp-settings-section-nice-icons-2
This ticket was mentioned in Slack in #core-test by nikunj8866. View the logs.
2 months ago
#11
@
2 months ago
Tested PR #11472 (commit 5144d56) against trunk in a local Docker environment (npm run env:start).
Manual test — used the snippet from comment:8 in options-reading.php:
add_settings_section( 'awesome-avatars', 'My avatars section title', '__return_false', 'andreatest' ); ... do_settings_sections( 'andreatest' );
Resulting markup:
<h2 id="wp-settings-section-awesome-avatars-1">My avatars section title</h2> <h2 id="wp-settings-section-nice-icons-2">My icons section title</h2>
The wp-settings-section- prefix + wp_unique_id() incrementing suffix works as described, and resolves the ID-collision concern raised in the PR review.
Core forms with static section headings — verified all 5 files touched by the patch render the expected id attributes and enable hash navigation:
options-media.php:#wp-settings-section-image-sizes,#wp-settings-section-uploading-filesoptions-writing.php:#wp-settings-section-post-via-email,#wp-settings-section-update-servicesoptions-discussion.php:#wp-settings-section-avatarsoptions-permalink.php:#wp-settings-section-common-settings,#wp-settings-section-optional
Automated checks:
Tests_Admin_IncludesTemplate(PHPUnit): 34 tests, 115 assertions, all passing — no regressions.- PHPCS lint on the 5 changed files: clean.
No issues found.
Patch tested successfully. I found no regressions, and the proposed fix behaves as expected.
#13
@
2 months ago
- Keywords commit added
On top of the linked PR, 65027.diff adds similar IDs to the headings of the Network Settings sections.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Trac ticket:
## Use of AI Tools
---
Ticket Details: https://core.trac.wordpress.org/ticket/65027
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.