diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index fb0dcffd42..5c7ce930e1 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2418,7 +2418,11 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { return new WP_Error('empty_term_name', __('A name is required for this term')); } $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); - $args = wp_parse_args($args, $defaults); + $args = wp_parse_args( $args, $defaults ); + + if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { + return new WP_Error( 'missing_parent', __( 'The selected parent term no longer exists' ) ); + } $args['name'] = $term; $args['taxonomy'] = $taxonomy; $args = sanitize_term($args, $taxonomy, 'db'); diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 8f01469f75..c95cbd41e3 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -690,4 +690,16 @@ class Tests_Term extends WP_UnitTestCase { $this->assertEquals( $tag_id, $terms[0]->term_id ); $this->assertEquals( 'This description is even more amazing!', $terms[0]->description ); } + + /** + * @ticket 19205 + */ + function test_orphan_category() { + $cat_id1 = $this->factory->category->create(); + + wp_delete_category( $cat_id1 ); + + $cat_id2 = $this->factory->category->create( array( 'parent' => $cat_id1 ) ); + $this->assertWPError( $cat_id2 ); + } }