WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#35321 closed defect (bug) (fixed)

Inserting a term with a description of null results in the term being created, but not its relationship to a taxonomy

Reported by: eclev91 Owned by: boonebgorges
Milestone: 4.5 Priority: normal
Severity: normal Version: 4.4
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

Given the following:

<?php
$t = wp_insert_term(
      'Foo',
      'bar',
      array(
          "description"=> null
      )
  );

And given that bar is a registered taxonomy:

With debug on, you get:

WordPress database error: [Column 'description' cannot be null]
INSERT INTO `wp_term_taxonomy` (`term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (119, 'bar', NULL, 0, 0)

And otherwise it fails silently.

The problem: The term is still created! If you dump $t you'll see:

Array
(
    [term_id] => 119
    [term_taxonomy_id] => 0
)

Expected behavior: Return a WP_Error on the SQL failure rather than create a term without a taxonomy.

Attachments (2)

35321.diff (607 bytes) - added by TimothyBlynJacobs 6 years ago.
35321-tests.diff (839 bytes) - added by TimothyBlynJacobs 6 years ago.

Download all attachments as: .zip

Change History (8)

#1 @swissspidy
6 years ago

  • Component changed from General to Taxonomy

#2 @TimothyBlynJacobs
6 years ago

I disagree that a WP_Error should be returned, it seems like passing NULL should be allowed and converted to an empty string. Though there should probably be some discussion on what happens to the newly created term if the subsequent term taxonomy insert fails.

#3 @TimothyBlynJacobs
6 years ago

  • Keywords has-patch added

#4 @dd32
6 years ago

  • Milestone changed from Awaiting Review to 4.5

Passing NULL really is an odd thing here IMHO, however, as this previously worked I see no harm in casting it.

The other option would be something like this instead:

if ( ! $args['description'] ) {
     $args['description'] = '';
}

#5 @eclev91
6 years ago

FWIW, it was a sync from an external API that may have a description of null or not. The plugin author could have written a check. It's not an issue that the taxonomy isn't created, just that the term is created without one

#6 @boonebgorges
6 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 36214:

Ensure 'description' is a string in wp_insert_term().

Passing 'description' => null when creating a term can cause MySQL notices,
as the description column in the terms table does not allow for null values.
We correct this by intepreting a null description as an empty string.

Props TimothyBlynJacobs.
Fixes #35321.

Note: See TracTickets for help on using tickets.