#53212 closed enhancement (fixed)
add post_type-specific version of the register_post_type_args filter
Reported by: | pbiron | Owned by: | dlh |
---|---|---|---|
Milestone: | 6.0 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Posts, Post Types | Keywords: | needs-docs has-patch has-unit-tests has-dev-note |
Focuses: | Cc: |
Description
register_post_type_args can be used to filter the args used when registering a post type.
I suggest we add a post_type-specific version of this filter, i.e., register_post_type_{$post_type}_args, similar to many other "dynamic" filter names.
This would allow developers that only want to modify the args for 1 given post type to simplify their callback function by not needing to check the post type being registered before modifying the args.
That is, instead of writing:
add_filter( 'register_post_type_args', 'modify_args', 10, 2 );
function modify_args( $args, $post_type ) {
if ( 'some_post_type' === $post_type ) {
$args['supports'][] = 'page-attributes';
}
return $args;
}
they could write the more straight forward:
add_filter( 'register_post_type_some_post_type_args', 'modify_args' );
function modify_args( $args, $post_type ) {
$args['supports'][] = 'page-attributes';
return $args;
}
Patch coming momentarily.
Attachments (1)
Change History (28)
#3
@
3 years ago
Having longed for a hook like this many times, I hope it won't blow the scope out of proportion to suggest that this effort might include a similar hook for after registration, and then equivalents of both hooks for taxonomies. So, object type-specific hooks for:
register_post_type_args
registered_post_type
register_taxonomy_args
registered_taxonomy
The use case in the description seems to me to apply to each of these hooks not-infrequently.
#4
@
3 years ago
I certainly have no objection :-) I opened the ticket just for register_post_type_args
because that was my immediate need (for a plugin I'm writing at the moment).
Before refreshing the patch to include the other new hooks, I'll wait for others (e.g., a committer) to weigh in on the general idea. But doing it for the other hooks you suggest will be very straightforward and could be useful for me as well.
This ticket was mentioned in Slack in #core by pbiron. View the logs.
3 years ago
#6
@
3 years ago
- Keywords needs-dev-note added
- Milestone changed from Awaiting Review to 5.8
- Owner set to davidbaumwald
- Status changed from new to accepted
Marking this for needs-dev-note
for at least a small call-out on the Misc Dev Note.
#7
@
3 years ago
Thanx @davidbaumwald.
Just let me know if you want me to update the patch to include the additional filters mentioned by @dlh.
#9
@
3 years ago
@pbiron I'm reviewing now. I think it's probably best to keep this ticket focused on the original scope, then open a follow-up ticket to add the other filters. I'm a little leery about adding too much at the Feature Freeze deadline without proper testing.
#10
@
3 years ago
Thanx.
I'm fine with keeping the focus as I originally stated it, as that solves my immediate need. And will be happy to open a follow up for the other filters.
If comment 2 needs to be addressed, I think that can be done after the feature freeze.
#11
@
3 years ago
- Milestone changed from 5.8 to 5.9
I'm moving this to 5.9 for more time to rollup the other filters and having some proper tests added for the new filters. Sorry for the false-start, @pbiron.
This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.
3 years ago
#13
@
3 years ago
- Keywords needs-unit-tests added
- Milestone changed from 5.9 to Future Release
Today is 5.9 feature freeze. With no activity in 6 months and work remaining to be done, punting this to 6.0, expect that milestone isn't available yet. Once it is, please feel free to move it into the milestone.
#14
@
3 years ago
- Keywords needs-patch added; has-patch removed
- Milestone changed from Future Release to 6.0
- Owner changed from davidbaumwald to dlh
- Status changed from accepted to assigned
Per discussion in Slack, I'll work on an early
patch that includes all of the proposed filters.
This ticket was mentioned in PR #2527 on WordPress/wordpress-develop by dlh01.
2 years ago
#16
- Keywords has-patch has-unit-tests added; needs-unit-tests needs-patch removed
Trac ticket: https://core.trac.wordpress.org/ticket/53212
#17
@
2 years ago
Well, it wasn't quite early
, but a PR for these hooks is now available here: https://github.com/WordPress/wordpress-develop/pull/2527
Feedback welcome, particularly on the location of the tests. If the consensus is that it's too late to consider all four hooks for 6.0, then I'll be happy to create a separate PR for just the hook originally proposed.
2 years ago
#19
Thanks for the review, @pbiron!
I think it would better if the post_type-specific filter were applied before register_post_type_args .
I searched for a pattern to follow in existing hooks but couldn't find much consistency. save_post_{$post->post_type}
comes before save_post
, but edited_term
comes before edited_{$taxonomy}
...etc.
My thinking was that adding the new hooks after the existing hooks would preserve the behavior of those existing hooks, framing the new hooks as customization points that "add-on" to the current logic. But I suppose I don't have much of a preference here. Happy to defer to the judgment of the component maintainers and committers.
I have a slight preference for the name of the new filters to be register_post_type_XXXX_args, but won't object if other folk like them this way
To me, register_post_type_XXXX_args
sounds like it could also be a filter for a specific registration argument, like register_post_type_rewrite_args
or something, whereas register_XXXX_post_type_args
puts the focus on "which post type?" So, I have a slight preference for the current approach. But maybe there's also an alternate reading of the current approach in the PR, too, that I'm not hearing 😄
I do feel more strongly, tho, that where ever the post type name appears in the name of the filter, it should be done as:
$post_type = $this->name; apply_filters( "register_{$post_type}_post_type_args", ... ); // or apply_filters( "register_post_type_{$post_type}_args", ... );Doing that makes the Code Reference cleaner (there has been a lot of that kind of cleanup for similar filters/actions recently).
Good to know! Updated in the latest commit.
peterwilsoncc commented on PR #2527:
2 years ago
#21
Merged in https://core.trac.wordpress.org/changeset/53126, thanks.
This ticket was mentioned in PR #2573 on WordPress/wordpress-develop by dlh01.
2 years ago
#22
See discussion at https://github.com/WordPress/wordpress-develop/pull/2527#discussion_r847490492.
Trac ticket: https://core.trac.wordpress.org/ticket/53212
#23
@
2 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
Apologies for reopening this ticket, but as @pbiron noticed, I neglected to update the inline docs for the new hooks after renaming the interpolated variables. I've opened a new PR with those updates, plus a couple other additions to the docs that Paul suggested: https://github.com/WordPress/wordpress-develop/pull/2573
peterwilsoncc commented on PR #2573:
2 years ago
#26
Committed in https://core.trac.wordpress.org/changeset/53170, thanks for following this up.
Related: #50734
Maybe the DocBlock for the new filter in the patch should be amended to mention all the built-in post types, as described in the related ticket?