diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 9865f42be7..78dd52ced9 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2579,8 +2579,13 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) ); } + $args['name'] = $term; $args['taxonomy'] = $taxonomy; + + // Coerce null description to strings, to avoid database errors. + $args['description'] = (string) $args['description']; + $args = sanitize_term($args, $taxonomy, 'db'); // expected_slashed ($name) diff --git a/tests/phpunit/tests/term/wpInsertTerm.php b/tests/phpunit/tests/term/wpInsertTerm.php index c3dea31247..386b66fa15 100644 --- a/tests/phpunit/tests/term/wpInsertTerm.php +++ b/tests/phpunit/tests/term/wpInsertTerm.php @@ -642,6 +642,23 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase { } + /** + * @ticket 35321 + */ + public function test_wp_insert_term_with_null_description() { + + register_taxonomy( 'wptests_tax', 'post' ); + + $term = wp_insert_term( 'foo', 'wptests_tax', array( + 'description' => null + ) ); + + $term_object = get_term( $term['term_id'] ); + + $this->assertInstanceOf( 'WP_Term', $term_object ); + $this->assertSame( '', $term_object->description ); + } + /** Helpers **********************************************************/ public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) {