WordPress.org

Make WordPress Core

#37655 closed enhancement (fixed)

[Template Hierarchy] category-slug.php cannot be properly loaded when slug is not English

Reported by: withgjr Owned by: johnbillion
Milestone: 4.7 Priority: normal
Severity: normal Version:
Component: Themes Keywords: has-patch has-unit-tests
Focuses: template Cc:

Description

This problem can be reproduced very easily:

  1. Create a category using the language other than English. (In my case, Chinese)
  2. And create a category-slug.php file.
  3. And then you can see the Wordpress can not find the right template to load.

This problem is caused by the function 'get_category_template' in the wp-includes/template.php file.

Just change this line:

<?php
if ( ! empty( $category->slug ) ) {
                $templates[] = "category-{$category->slug}.php";
                $templates[] = "category-{$category->term_id}.php";
}

To:

<?php
if ( ! empty( $category->slug ) ) {
                $decoded_slug = urldecode($category->slug);
                $templates[] = "category-{$decoded_slug}.php";
                $templates[] = "category-{$category->term_id}.php";
}

And this problem can be solved.

Attachments (4)

37655.patch (568 bytes) - added by withgjr 14 months ago.
37655.diff (568 bytes) - added by withgjr 14 months ago.
37655.2.diff (2.0 KB) - added by johnbillion 14 months ago.
37655.3.diff (7.0 KB) - added by johnbillion 14 months ago.

Download all attachments as: .zip

Change History (13)

@withgjr
14 months ago

@withgjr
14 months ago

#1 @swissspidy
14 months ago

  • Component changed from General to Taxonomy
  • Focuses template added
  • Keywords has-patch added
  • Version 4.5.3 deleted

#2 @withgjr
14 months ago

Sorry, I have no experience on contributing Wordpress before. The newest 37655.diff file is the correct version. I don't know how to remove the wrong file '37655.patch'.

#3 follow-up: @swissspidy
14 months ago

  • Keywords needs-unit-tests added

@withgjr Thank you very much for your contribution! 37655.diff is perfectly fine.

get_category_template() isn't the only function that would need need to be adjusted though. Also, automated tests are required to verify the existence of the bug and how the patch fixes it.

If you have experience in writing tests, any help is greatly appreciated. If not, some sample data to reproduce this (i.e. title & slug of the category you created) would be very helpful as well. That way, someone else can chime in and contribute tests.

#4 in reply to: ↑ 3 @withgjr
14 months ago

Chinese is my mother tongue, and I also can speak some Korean, so I choose this two languages as sample data.

I choose the word 'test' for this two language.

  1. 測試
  2. 테스트

You can use the words above as slug and title to reproduce the bug.

Replying to swissspidy:

@withgjr Thank you very much for your contribution! 37655.diff is perfectly fine.

get_category_template() isn't the only function that would need need to be adjusted though. Also, automated tests are required to verify the existence of the bug and how the patch fixes it.

If you have experience in writing tests, any help is greatly appreciated. If not, some sample data to reproduce this (i.e. title & slug of the category you created) would be very helpful as well. That way, someone else can chime in and contribute tests.

#5 @johnbillion
14 months ago

  • Milestone changed from Awaiting Review to 4.7
  • Owner set to johnbillion
  • Status changed from new to reviewing

#6 @johnbillion
14 months ago

37655.2.diff introduces the decoded form of template file names as an extra, higher-priority entry within the template hierarchy. This means that existing templates that use the URL-encoded form of the template name will continue to work.

Example:

Given a page with a slug of 測試, the page template hierarchy now looks like:

  1. page-測試.php
  2. page-%e6%b8%ac%e8%a9%a6.php
  3. page-123.php
  4. page.php
  5. singular.php
  6. index.php

This applies to category archives, tag archives, taxonomy term archives, pages, and posts.

Tests are a work in progress and are dependent on #14310 which I hope to get in soon.

#7 @johnbillion
14 months ago

  • Keywords has-unit-tests 2nd-opinion added; needs-unit-tests removed

37655.3.diff is an updated patch with tests which demonstrate the updated template hierarchy when a post or term name includes multibyte characters. See comment:6 for the details.

I'd love to get this in. Any feedback from anyone?

#8 @johnbillion
13 months ago

  • Component changed from Taxonomy to Themes
  • Keywords 2nd-opinion removed
  • Type changed from defect (bug) to enhancement

#9 @johnbillion
13 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 38583:

Themes: Add the non-encoded form of the queried item slug to the template hierarchy when the slug contains non-ASCII characters.

This affects category, tag, and custom taxonomy archives, and single posts, pages, and custom post types.

Fixes #37655

Note: See TracTickets for help on using tickets.