Make WordPress Core

Opened 7 years ago

Last modified 2 months ago

#49194 reviewing defect (bug)

Page Template dropdown ordering should be by title, not value

Reported by: leec87 Owned by:
Priority: normal Milestone: Future Release
Component: Posts, Post Types Version:
Severity: minor Keywords: good-first-bug has-test-info has-patch has-unit-tests
Cc: Focuses: ui, administration

Description

The ordering of items within the Page Template dropdown field is sorted by the value alphabetically, which often creates what appears to be an unordered display when viewing on the front end.

I this should be changed so the ordering of the dropdown options is based on the alphabetical order of the list items themselves.

Attachments (4)

template-dropdown-source.png​ (63.2 KB ) - added by leec87 7 years ago.
sort-page-templates.patch​ (1.1 KB ) - added by johnnycocheroo454 14 months ago.
Proposed fix patch file
49194-sort-page-templates-by-title.patch​ (935 bytes ) - added by sachinrajcp123 9 months ago.
49194-sort-p…tes-by-title.diff​ (2.4 KB ) - added by mattwatsoncodes 5 months ago.
Patch to sort post templates by translated, human-readable template name instead of filename/slug, with PHPUnit test coverage.

Download all attachments as: .zip

Change History (22)

#1 @SergeyBiryukov
7 years ago

  • Component General → Posts, Post Types
  • Focuses ui administration added; accessibility removed

This ticket was mentioned in ​Slack in #core by sirlouen. ​View the logs.


14 months ago

#3 @SirLouen
14 months ago

  • Keywords good-first-bug needs-patch has-test-info added
  • Milestone Awaiting Review → Future Release
  • Type enhancement → defect (bug)

Reproduction Report

Description

✅ This report validates that the issue can be reproduced.

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.29
  • Server: nginx/1.29.1
  • Database: mysqli (Server: 8.4.6 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 140.0.0.0
  • OS: Windows 10/11
  • Theme: Minimal Theme 1.0.0
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0

Testing Instructions

  1. I'm using a very minimal theme ​like this
  2. Then as commented in the OP, created the 3 templates, like a-template.php, b-template.php, c-template.php
  3. Finally add the code in the supp artifacts to add a menu item to display with get_page_templates
  4. 🐞 Exactly as commented, ordering is done by file name and not by template name as it should

Actual Results

  1. ✅ Error condition occurs (reproduced).

Supplemental Artifacts

Added this to functions.php:

function page_templates_admin_menu() {
    add_menu_page(
        'Page Templates',
        'Page Templates',
        'manage_options',
        'page-templates',
        'page_templates_admin_page',
        'dashicons-media-code',
        81
    );
}
add_action( 'admin_menu', 'page_templates_admin_menu' );
function show_page_templates() {
    
}


function page_templates_admin_page() {
    $templates = get_page_templates();
    echo '<div>';
    echo '<h1>Page Templates</h1>';
    
    echo '<ul>';
    foreach ( $templates as $filename => $name ) {
        echo '<li>' . $name . ' (' . $filename . ')</li>';
    }
    echo '</ul>';
    echo '</div>';
}

#4 @johnnycocheroo454
14 months ago

Proposed patch sorts page templates by their human-readable name before flipping the array, ensuring UI ordering by template name, patch file uploaded.

#5 @SirLouen
14 months ago

  • Keywords has-patch added; needs-patch removed
  • Owner set to SirLouen
  • Status new → reviewing

Thanks @johnnycocheroo454 I will be reviewing it asap.

#6 @SirLouen
9 months ago

  • Owner SirLouen removed

#7 @ant1busted
8 months ago

Test Report

Description

This report validates whether the indicated patch works as expected.
The patch tested was johnnycocheroo454 from @johnnycocheroo454
I tried applying 49194-sort-page-templates-by-title.patch but it failed. The modified file is not the same as in the patch.

Patch tested: https://core.trac.wordpress.org/ticket/49194

Environment

  • WordPress: 7.0-beta2-20260305.115512
  • PHP: 8.2.27
  • Server: nginx/1.26.1
  • Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.2.27)
  • Browser: Firefox 148.0
  • OS: Linux
  • Theme: Twenty ten
  • MU Plugins: None activated
  • Plugins: None
    • Test Reports 1.2.1

Actual Results

  1. ❌ Failed : The list was still following the value and not the name.

Additional Notes

  • Any additional details worth mentioning.

Supplemental Artifacts

Screen captures here : ​https://imgur.com/a/N92QM9c

This ticket was mentioned in ​PR #11955 on ​WordPress/wordpress-develop by ​@kazisadibreza.


6 months ago
#8

Sorted the resolved page and block template arrays alphabetically by their human-readable, translated titles using a case-insensitive uasort() loop. This resolves an issue where templates were being returned based on raw filesystem file discovery sorting orders, causing mismatched structural dropdown hierarchies in the editor interface UI.

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

## Use of AI Tools

AI assistance: Yes
Tool(s): Gemini
Model(s): Gemini 1.5 Pro
Used for: Identifying legacy variable extraction errors and structuring the uasort callback implementation within the get_post_templates() method context.

@mattwatsoncodes
5 months ago

Patch to sort post templates by translated, human-readable template name instead of filename/slug, with PHPUnit test coverage.

#9 @mattwatsoncodes
5 months ago

Added 49194-sort-post-templates-by-title.diff.

This takes the same approach as the existing PR by sorting the resolved $post_templates arrays after translation and after custom block templates are merged, but simplifies the callback to use strnatcasecmp directly.

The patch also adds PHPUnit test coverage to confirm templates are sorted by their human-readable template labels while preserving the original template keys.

This ticket was mentioned in ​Slack in #core-test by softglaze. ​View the logs.


3 months ago

#11 @ekamran
3 months ago

  • Keywords needs-refresh added

Patch Testing Report

Patch tested: 49194-sort-p…tes-by-title.diff​ (by @mattwatsoncodes)

Environment

  • WordPress: 7.1-RC3
  • PHP: 8.3.31
  • Server: Apache/2.4.67 (Debian, official Docker image)
  • Database: MariaDB 12.3.2 (mysqli)
  • Browser: Chrome 148
  • OS: macOS 26.5.2 (Docker host)
  • Theme: custom minimal classic theme made for this test
  • MU-Plugins: None activated
  • Plugins: None activated

Steps taken

  1. Created a classic theme where filename order is opposite of template name order: a-template.php → "Zebra Template", b-template.php → "Mango Template", c-template.php → "apple template" (lowercase kept on purpose), d-template.php → "Banana Template". Also added "Section 2" / "Section 10" for natural sort check, and two templates with Template Post Type: book for a custom post type.
  2. On clean 7.1-RC3, confirmed the bug — get_page_templates(), WP_Theme::get_post_templates() and the block editor Template dropdown all show filename order (Zebra, Mango, apple, Banana).
  3. Applied the core hunk to wp-includes/class-wp-theme.php with patch -p2 (applied at offset of 9 lines on RC3, otherwise clean).
  4. Re-checked everything after patch — both APIs, block editor dropdown for pages, and for the book CPT. All are now in template name order.
  5. Edge cases all fine — case-insensitive ("apple template" comes first), natural sort ("Section 2" before "Section 10"), block theme custom templates not affected (checked with Twenty Twenty-Five), template assignment saves and renders properly, debug.log clean.
  6. Ran PHPUnit on wordpress-develop trunk — the new test in the patch passes with the fix and fails without it, so the test is valid.
  7. ✅ Patch is solving the problem: dropdowns and APIs now list templates alphabetically by the visible template name. ⚠️ But please note, the patch's new test fixtures are breaking one existing PHPUnit test (details below) — needs a small revision before commit.

Expected result

Page Template dropdown should be ordered alphabetically by the visible template name, not by the filename.

Additional Notes

  • Running the wider theme test suite (349 tests) gives 1 failure caused by this patch: Tests_Theme_wpThemeGetPostTemplates::test_get_post_templates_child_theme (#41717). The patch puts its fixture files (template-a-zebra.php, template-z-alpha.php) inside the shared themedir1/page-templates theme, and page-templates-child inherits them, so that test's expected arrays don't match anymore. Not a runtime issue — but CI will go red as submitted. Fix is simple: either update the expected arrays in that test, or keep the new fixtures in a separate test theme.
  • 49194-sort-page-templates-by-title.patch​ does not apply — file is malformed (CRLF line endings, whitespace broken). Same as what @ant1busted reported earlier.
  • sort-page-templates.patch​ applies but fixes only the get_page_templates() wrapper in wp-admin. Block editor bypasses it (edit-form-blocks.php calls wp_get_theme()->get_page_templates() directly), so the editor dropdown stays unsorted. This most probably explains the earlier failed test report.
  • PR #11955 does the same thing as the tested patch, with same runtime behaviour, but has no test coverage.
  • One more observation — page_template_dropdown() in wp-admin/includes/template.php still does its own case-sensitive, non-natural ksort(). So after this patch, Quick Edit order can differ from block editor order (e.g. "Section 10" before "Section 2" there). Since sorting is now inside get_post_templates(), that ksort() looks redundant — happy to test its removal as a follow-up if needed.

This ticket was mentioned in ​PR #13383 on ​WordPress/wordpress-develop by ​@rishabhwp.


2 months ago
#12

  • Keywords has-unit-tests added; needs-refresh removed

The Page Template dropdown lists templates in filename order instead of by their human-readable "Template Name". This sorts WP_Theme::get_post_templates() by the translated name, so the dropdown (and every other consumer) is ordered by what the user actually sees.

Uses uasort() with strnatcasecmp — case-insensitive, natural order ("Section 2" before "Section 10"), file name keys preserved. Sorting happens after translation so it respects the active locale.

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

## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Code Review

#13 @ekamran
2 months ago

  • Keywords needs-refresh added

Patch Testing Report

Patch tested: PR 13383 by @rishabhwp (head 58a1a05d8f)

Environment:
Unit tests on the wordpress-develop PR branch. Runtime checked on two independent stacks: WordPress 7.1 (WordPress Studio, nginx, MySQL 8, PHP 8.5) and WordPress 6.9.4 (official Docker image, Apache, MariaDB 11), each with the class-wp-theme.php change applied by hand. Theme: the PR's own page-templates-sort fixture. No plugins.

Results:

  1. The new unit test passes with the patch and fails without it, showing the old file name order with section-10.php before section-2.php. The themes group shows no regressions from the patch.
  1. Runtime, both stacks, identical output. After the patch get_page_templates() returns Apple Template, mango template, Section 2, Section 10, Zebra Template. Correct order, so the patch works as described.
  1. The dropdown from this ticket does not change. page_template_dropdown() re-sorts with ksort( $templates ) in wp-admin/includes/template.php (line 914 on trunk), a case sensitive byte sort on the names. So the dropdown still shows Section 10 before Section 2 and mango template after Zebra Template. Both admin consumers go through it: the Page Attributes meta box and Quick Edit.

Conclusion: the sort in WP_Theme::get_post_templates() is the right place, but the dropdown only changes if the ksort() call is removed together with this patch. Removing ksort() alone would regress to file name order, so the two belong together. Happy to test an updated patch that covers both.

#14 @rishabhwp
2 months ago

  • Keywords needs-refresh removed

Thanks for the thorough testing, @ekamran.

You're right, page_template_dropdown() re-sorts the templates with ksort(), so the order from get_post_templates() gets changed again in the Page Attributes meta box and Quick Edit.

I've updated the PR to remove the ksort() from page_template_dropdown() so it keeps the order returned by get_post_templates(). This keeps the template order consistent across the block editor, Page Attributes, and Quick Edit.

Would appreciate a re-test when you get a chance. Thanks!

#15 @ekamran
2 months ago

Patch Testing Report

Patch tested: PR 13383 by @rishabhwp (head cb6e3667, with the ksort() removal)

Environment:
Unit tests on the wordpress-develop PR branch. Runtime checked again on the same two stacks as my last report: WordPress 7.1 (WordPress Studio, nginx, MySQL 8, PHP 8.5) and WordPress 6.9.4 (official Docker image, Apache, MariaDB 11), with both changed files applied. Theme: the PR's page-templates-sort fixture. No plugins.

Results:

The new unit test passes with the patch and fails without it. The full themes group passes, 628 tests, including test_get_post_templates_child_theme.

Runtime, both stacks, identical output. Before the patch, get_page_templates() returns file name order and the dropdown shows the old ksort() order, with Section 10 before Section 2 and mango template last. After the patch, both return Apple Template, mango template, Section 2, Section 10, Zebra Template.

The dropdown now matches get_post_templates() exactly, so the Page Attributes meta box and Quick Edit are consistent with the block editor. Those two are the only core callers of page_template_dropdown(), so nothing else consumed the removed ksort() order.

For comparison, PR 11955 and the older attachments change only WP_Theme::get_post_templates(), so with any of those the dropdown keeps the old ksort() order. PR 13383 is the only one that fixes the dropdown this ticket is about.

Conclusion: the updated patch fixes the ticket's dropdown on both stacks. The mismatch between the API order and the dropdown order from my last report is gone. One optional improvement: the added test pins get_post_templates() but not the dropdown itself, so a small test asserting the option order from page_template_dropdown() would keep the second half pinned too. Works for me.

​@rishabhwp commented on ​PR #13383:


2 months ago
#16

Hi @ekamran, thanks for the detailed testing and confirmation.

I’ve added a test asserting the page_template_dropdown() option order. This keeps both the get_post_templates() API order and the dropdown order covered by tests.

Let me know if you have any further feedback.

#17 @ekamran
2 months ago

Patch Testing Report

Patch tested: PR 13383 by @rishabhwp (head e8f7eb9bb1, adds the drop-down order test)

Environment:
Unit tests on the wordpress-develop PR branch, same rig as my last report. The source files are unchanged since the previous head; only the new test was added, so the runtime results from my last report still apply.

Results:

The new drop-down test passes with the patch. With the ksort() restored, it fails showing the old order, Section 10 before Section 2, so it pins exactly the behavior this ticket is about.

The full Tests_Admin_IncludesTemplate class passes locally, 43 tests. The themes group passes locally, 628 tests.

Conclusion: the drop-down order is now covered by a test as suggested. The runtime mismatch between get_post_templates() and page_template_dropdown() remains fixed from the previous head. Works for me.

​@ekamran commented on ​PR #13383:


2 months ago
#18

@SirLouen tagging you as the Posts, Post Types component maintainer, since you triaged this ticket earlier and started a review. The PR now fixes both layers found during patch testing: WP_Theme::get_post_templates() sorts by translated name, and page_template_dropdown() no longer re-sorts with ksort(). It also includes a test pinning the drop-down order. Testing notes are on Trac: https://core.trac.wordpress.org/ticket/49194

Note: See TracTickets for help on using tickets.