#28683 closed enhancement (fixed)
Notify developers when register_post_type() fails because $post_type exceeds 20 characters
Reported by: |
|
Owned by: |
|
---|---|---|---|
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)
Change History (11)
This ticket was mentioned in IRC in #wordpress-dev by mattheweppelshei. View the logs.
11 years ago
#4
@
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.
#6
@
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
@
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
@
11 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 28902:
This 28683.diff patch adds a
_doing_it_wrong()
call to thestrlength()
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.