Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#36387 closed defect (bug) (duplicate)

Function: term_exists() fails on children with ampersand

Reported by: howdy_mcgee's profile Howdy_McGee Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4.2
Component: Taxonomy Keywords:
Focuses: administration, template, performance Cc:

Description

This may be a very specific case but it's 100% replicable. Post Categories should look like this:

Foo & Bar ( Slug: foo-bar )

  • Foo & Bar ( Slug: foo-bar-foo-bar )

Parent and child have the same name with an ampersand. Then you check if each term exists in their respective parents:

term_exists( 'Foo & Bar', 'category', 0 );

Returns expected results of the Term Array. The issue comes when you pass the parents ID:

term_exists( 'Foo & Bar', 'category', $parent_id );

Returns NULL which is an issue as if you're trying to insert a term if it doesn't exists using wp_insert_term() you get a WP Error:

A term with the name provided already exists with this parent.

If I change the above ampersands to 'and' I don't run into this issue.

Change History (1)

#1 @boonebgorges
8 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi @Howdy_McGee - Thanks for the ticket.

This issue has been discussed previously in https://core.trac.wordpress.org/ticket/32248#comment:9, and broadly falls under #11311. Briefly, when you create a tag Foo & Bar through the post.php interface (or elsewhere in the Dashboard, I think), sanitization routines run on the name field before it's saved. Specifically, sanitize_term_field() causes _wp_specialchars() to HTML-encode the ampersand. So Foo & Bar in the interface ends up as Foo & Bar in the database.

Whether it's wise for us to be HTML-encoding term names before saving is a question for #11311. For your purposes, you should run raw content through sanitize_term_field() before passing to term_exists(), so that your call looks like term_exists( 'Foo & Bar' )

Note: See TracTickets for help on using tickets.