Ticket #13709 (closed defect (bug): fixed)

Opened 2 years ago

Last modified 18 months ago

Post type name should be limited to 20 chars in register_post_type()

Reported by: phrostypoison Owned by:
Priority: normal Milestone: 3.1
Component: Post Types Version:
Severity: normal Keywords:
Cc: kevinB, johnpbloch

Description

I was in the process of making a plugin for WordPress 3.0 that utilizes a custom post_type with the name 'designtags_stylesheet', but when I activated the plugin, although the UI would show up properly, the publish button showed 'Submit for Review' instead of the usual 'publish'. Furthermore, if I do click on the button, WordPress spits out an error and does nothing.

The same situation didn't occur with kovshenin.org's Podcasts post_type plugin example (see  http://kovshenin.com/archives/extending-custom-post-types-in-wordpress-3-0/).

It turns out the cause of the problem was the length of the post_type name. After looking into the database schema of the wp_posts table, the post_type column has a 20-character limit. 'designtags_stylesheet' is more than 20 chars long, so WordPress refuses to publish.

If the post_type column is to stay as a varchar(20) after custom post_type support, register_post_type() should spit an error if a plugin attempts to register a post_type that has a name over 20 chars long.

Maybe change these lines

	$post_type = sanitize_user($post_type, true);
	$args->name = $post_type;

into

	$post_type = sanitize_user($post_type, true);
	if ( strlen($post_type) > 20)
		return new WP_Error('invalid_post_type', __('Invalid post type name.'));
	$args->name = $post_type;

Thank you!

Attachments

13709.patch Download (414 bytes) - added by johnpbloch 18 months ago.

Change History

I can't think of an obvious place where we check against the schema, I imagine somewhat rooted in portability. Related is a ticket that probably does deserve a schema sanity check: #11959.

Offtopic, but it's kovshenin.com sir, not .org ;)

  • Cc kevinB added
  • Milestone changed from Awaiting Review to 3.1

My patch is a pretty quick and simple fix. I don't think it's necessary to send a warning to the user, since we already potentially change the post_type key with sanitize_key(). The problem, as I understand it, is that this bug breaks WordPress without making it clear that the length of the post_type name is the issue (i.e. almost all user-facing uses of the post_type name use the name in its full and invalid length). Trimming the key at registration fixes that problem and would remove the confusion.

  • Cc johnpbloch added

johnpbloch — the problem is that there are many places where you can provide a post type, and your patch would just sanitize one of them. I'd rather let people know what's going on so they can choose a better post type name.

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

(In [16650]) Throw WP_Error if post_type passed to register_post_type() is longer than schema allows. props phrostypoison. fixes #13709

(In [16668]) Doh. [16646] and [16650] were made against the wrong branch. Revert. see #13709. see #15621

(In [16670]) Throw WP_Error if post_type passed to register_post_type() is longer than schema allows. props phrostypoison. fixes #13709

Note: See TracTickets for help on using tickets.