diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 3d1227ff9a..1e5d3a27f0 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -1342,9 +1342,9 @@ function register_post_type( $post_type, $args = array() ) { $post_type = sanitize_key( $post_type ); $args->name = $post_type; - if ( strlen( $post_type ) > 20 ) { - _doing_it_wrong( __FUNCTION__, __( 'Post types cannot exceed 20 characters in length' ), '4.0' ); - return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) ); + if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { + _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2' ); + return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); } // If not set, default to the setting for public. diff --git a/tests/phpunit/tests/post/types.php b/tests/phpunit/tests/post/types.php index 0c8d95d298..e1582d5d8d 100644 --- a/tests/phpunit/tests/post/types.php +++ b/tests/phpunit/tests/post/types.php @@ -19,6 +19,26 @@ class Tests_Post_Types extends WP_UnitTestCase { _unregister_post_type( 'foo' ); } + /** + * @ticket 31134 + * + * @expectedIncorrectUsage register_post_type + */ + function test_register_post_type_length_long() { + // post type too long + $this->assertInstanceOf( 'WP_Error', register_post_type( 'abcdefghijklmnopqrstuvwxyz0123456789' ) ); + } + + /** + * @ticket 31134 + * + * @expectedIncorrectUsage register_post_type + */ + function test_register_post_type_length_short() { + // post type too short + $this->assertInstanceOf( 'WP_Error', register_post_type( '' ) ); + } + function test_register_taxonomy_for_object_type() { global $wp_taxonomies;