Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#28683 closed enhancement (fixed)

Notify developers when register_post_type() fails because $post_type exceeds 20 characters

Reported by: mattheweppelsheimer's profile mattheweppelsheimer Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 4.0 Priority: normal
Severity: normal Version: 3.1
Component: Posts, Post Types Keywords: has-patch dev-feedback
Focuses: Cc:

Description

register_post_type() parameter $post_type can't exceed 20 characters. The function returns a WP_Error if the limit is exceeded. Both the limit and the error are fairly well documented.

This error is silent unless you think to check for it, which (anecdotally) seems uncommon in practice (for instance, example code on the Codex doesn't check the return result). This caused me a few hours of frustration before I figured it out, and seems to be a common pain point even amongst more experienced WP devs. There's definitely opportunity to further improve the PHPDoc for register_post_type()… but I don't think that will really resolve this pain point.

When developers stumble into this silent failure, I'd like to us to notify them proactively, in a way that's harder to miss.

Thoughts?

FYI the WP_Error return result was introduced in 3.1. Hence the Version tag.

Attachments (1)

28683.diff (670 bytes) - added by mattheweppelsheimer 11 years ago.

Download all attachments as: .zip

Change History (11)

#1 @mattheweppelsheimer
11 years ago

  • Keywords has-patch dev-feedback added

This 28683.diff patch adds a _doing_it_wrong() call to the strlength() check. It still returns the error so the function signature is preserved.

I'm not sure if this patch is the best approach, but it is one way to get the outcome I would like.

Not that they've endorsed my proposal, but thanks to @dh-shredder, @drewapicture, and @maxcutler for some hand holding.

This ticket was mentioned in IRC in #wordpress-dev by mattheweppelshei. View the logs.


11 years ago

#3 @SergeyBiryukov
11 years ago

This applies to register_taxonomy() too, see #21593.

#4 @jeremyfelt
11 years ago

I've been bitten by this several times. It does seem common to not check the response of register_post_type and this would be pretty helpful. There is precedence in something like add_theme_support() which returns false and uses _doing_it_wrong() if an array of types is not passed.

#5 @SergeyBiryukov
11 years ago

  • Milestone changed from Awaiting Review to 4.0

#6 @jorbin
11 years ago

This seems like a great use case for _doing_it_wrong.

A two minor things that need to be cleaned up in the patch:
The new line 1246 has an extra space at the front of it.
4.0 as the version should be passed in as a string rather then as a float.

#7 @DrewAPicture
11 years ago

Talked to @mattheweppelsheimer in person at WC Seattle and I agree throwing a _doing_it_wrong() is the best approach.

The docs already reflect the limitation fairly well, and I think it's been established that few if any devs really even check the result of register_post_type() or register_taxonomy() for that matter.

+1 (including @jorbin's changes).

#8 @SergeyBiryukov
11 years ago

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

In 28902:

Notify developers when register_post_type() or register_taxonomy() fails because of post type or taxonomy key length.

props mattheweppelsheimer.
fixes #28683.

#9 @SergeyBiryukov
11 years ago

In 28926:

Add @expectedIncorrectUsage to Tests_Taxonomy::test_register_long_taxonomy().

see #28683.

This ticket was mentioned in Slack in #core by mattheweppelsheimer. View the logs.


10 years ago

Note: See TracTickets for help on using tickets.