Ticket #16567 (new defect (bug))

Opened 15 months ago

Last modified 15 months ago

Can't create single-character categories

Reported by: jeffreymcmanus Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version: 3.0.5
Severity: normal Keywords: has-patch needs-testing
Cc:

Description

We run a blog that deals with programming languages. Today we were creating a post concerning the C programming language and attempted to create a new category for it, called "C" (just one character). This angered WordPress; it appeared to create a superfluous "Uncategorized" category, but it wound up not actually creating any categories for the post.

I've attached a screenie so you can see what this looks like after I attempt to create the category.

If it's for some reason forbidden to create categories comprised of a single character, maybe a validation error message of some kind would be in order?

Attachments

wordpress_c_category_doh.png Download (29.2 KB) - added by jeffreymcmanus 15 months ago.
Screen shot of error
16567.diff Download (742 bytes) - added by garyc40 15 months ago.

Change History

Screen shot of error

I can reproduce that, if I try to create a category with a name resembling another category but only through the Add New Category on the Add New Post screen. Creating the category through Posts > Categories succeeds. So the problem is not the single-character category name in general.

Try to create the category through Posts > Categories to see if it works.

The bug is simple to reproduce. Just try to create a category that already exists or something like C, C#, C+, C! etc. (they have to be parent categories). "Uncategorized" is constantly appearing, but nothing is really created (try to click one of those "Uncategorized" and all of them are chosen) and refreshing the page makes them disappear.

This applies to WP 3.1-RC4 too.

Confirmed that it is possible to create a single-letter category through Posts > Categories (which I never knew existed until today!).

Can't reproduce on trunk (currently on r17453).

Wait, I can reproduce now. It happens when there's already a "C#" category, and you try to add "C".

  • Keywords has-patch needs-testing added

The conditional check on line 263, admin-ajax.php seems to be unnecessary, and introduces edge case like this.

Removing the conditional check seems to fix this problem and produce no side effects (the same kind of check is performed within wp_insert_term, and it handles the case better by creating the term anyways with a numerical suffix).

Attached patch does this 16567.diff Download.

In case someone wonders why "Uncategorized" is output, it's because the return value of term_exists() can also be an array if a taxonomy is specified (wp-includes/taxonomy.php:1438). wp_terms_checklist() later casts this value as an int, making it 1 because it's an array. As a result, what you get is whatever category has the ID 1 (in this case, Uncategorized).

By the way, the inline documentation of term_exists() states that:

@return mixed Get the term id or Term Object, if exists.

Judging from the code I see, the return value could only be either an associative array, or an integer, but never a term object. Or did I miss something?

Note: See TracTickets for help on using tickets.