Make WordPress Core

Opened 2 years ago

Last modified 14 months ago

#22487 new enhancement

Adding priority to settings sections.

Reported by: alexmansfield Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch settings-api 3.6-early
Focuses: administration Cc:


Currently there is no way (at least that I could find) to specify the order of settings sections on a settings page (for example, a theme options page). Use case: a child theme adding a settings section to the parent theme's "Theme Options" page has no way of specifying where on the page the new section should be placed.

I've modified the add_settings_section() function to allow for a priority to be set (much like when adding sections to the theme customizer). I also added uasort($wp_settings_sections[$page], 'compare_priority'); to the do_settings_section() function, where 'compare_priority' is a slightly modified version of the compare function used to set the priority of the theme customizer sections. The patch is based on version 3.4.2, but I'd be happy to make another for the development version if that would help.

This is my first patch submitted, so please let me know if I need to do anything differently. Thanks!

Attachments (1)

section-priority.patch (1.9 KB) - added by alexmansfield 2 years ago.
Adds priority option to settings sections

Download all attachments as: .zip

Change History (24)

@alexmansfield2 years ago

Adds priority option to settings sections

comment:1 @toscho2 years ago

  • Cc info@… added

comment:2 @DrewAPicture2 years ago

  • Cc xoodrew@… added

comment:4 follow-up: @kovshenin2 years ago

#20733 is for the Customizer, which already has priorities. This is about the Settings API I guess. It would also be nice if fields could have priorities too.

Also related: #18285 New Settings API & Workflow

comment:5 in reply to: ↑ 4 @SergeyBiryukov2 years ago

Replying to kovshenin:

#20733 is for the Customizer, which already has priorities.

It describes a bug which we'd probably want to fix before using a similar approach for Settings API.

comment:6 @alexmansfield2 years ago

@kovshenin Correct, this is for the settings API, not the Customizer. I would love to add priority to the individual fields as well, but I figured I'd start with the sections.

@SergeyBiryukov Yes, since I borrowed some of the code from the customizer, it would inherit the same the bug. However, I read the comments for #20733 and it was determined that the bug was relatively minor and was not a roadblock to release, so I don't think it would need to be a roadblock to implementing priorities in the settings API. As soon as the bug is solved for the customizer it could be ported to the settings API as well.

comment:7 @unknowndomain2 years ago

  • Keywords settings-3.6 added

comment:8 @unknowndomain2 years ago

  • Cc me@… added

comment:9 @ctsttom2 years ago

  • Keywords 3.6 needs-refresh added; has-patch removed

@alexmandfield would you be interested in refreshing this code once 3.5.1 is out in the next couple of weeks to get this into 3.6?

comment:10 @ctsttom2 years ago

  • Keywords 3.6-early added; 3.6 removed

comment:11 @SergeyBiryukov2 years ago

  • Keywords has-patch settings-api added; settings-3.6 removed

comment:12 @SergeyBiryukov2 years ago

  • Keywords needs-refresh removed

section-priority.patch still applies cleanly.

comment:13 follow-up: @alexmansfield2 years ago

So if I understand the changes added above, I don't need to rebuild the patch. Correct?

comment:14 in reply to: ↑ 13 @SergeyBiryukov2 years ago

Replying to alexmansfield:

So if I understand the changes added above, I don't need to rebuild the patch. Correct?

Yes. "@since 3.4.0" can be updated once the ticket is actually slated for a milestone.

comment:15 @unknowndomain2 years ago

@alexmandfield I have just tested the patch and found that it doesn't work as expected, if you have two sections with the same or default priority they should be ordered in the order in code, however they seem to get sorted backwards.

Also I think this should be expanded to add priority toadd_settings_fields as this could also be useful to people, especially once the Settings API is implemented in wp-admin.

What do you think?

comment:16 @alexmansfield2 years ago

@unknowndomain The code for setting the order comes from the code already used to set the order of Theme Customizer sections. This code has a known bug when priorities are the same (#20733). I figured that once that bug is resolved, I would simply incorporate the solution here as well.

Also, I agree that priorities should be expanded to add_settings_fields as well, I just haven't had the time to submit a patch for that yet. Thanks for the input!

Last edited 2 years ago by SergeyBiryukov (previous) (diff)

comment:17 @unknowndomain2 years ago

@alexmansfield I see, that explains a lot, they pointed out as I pondered, looking at the code you did if the way the settings are stored should be changed internally to match the way actions and filters work, i.e. they are stored as a multidimensional array $settings[priority][page][section][field].

Seems like a rather big change though considering how much impact it might have.

On a side note, do you think you will have time to add priority to settings field as you know what changes need to be done already?

Version 0, edited 2 years ago by unknowndomain (next)

comment:18 @alexmansfield2 years ago

I am hoping to submit a patch for settings fields next week, though I can't guarantee that I'll get it done by then. It's definitely on my list of things to do as soon as possible :)

comment:19 @unknowndomain2 years ago

Awesome, just means I can focus on the other settings-api stuff.

comment:20 @alexmansfield2 years ago

To those of you who mentioned adding priority to settings (rather than just sections), I've posted a patch for prioritizing settings here: #23387

Last edited 2 years ago by SergeyBiryukov (previous) (diff)

comment:21 @nacin15 months ago

  • Component changed from General to Plugins

comment:22 @nacin14 months ago

  • Component changed from Plugins to Admin APIs
  • Focuses administration added

comment:23 @nacin14 months ago

  • Component changed from Admin APIs to Plugins

Sorry for the noise.

Note: See TracTickets for help on using tickets.