Make WordPress Core

Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#53212 closed enhancement (fixed)

add post_type-specific version of the register_post_type_args filter

Reported by: pbiron's profile pbiron Owned by: dlh's profile 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:


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)

53212.diff (1.3 KB) - added by pbiron 3 years ago.

Download all attachments as: .zip

Change History (28)

3 years ago

#1 @pbiron
3 years ago

  • Keywords has-patch added

#2 @pbiron
3 years ago

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?

#3 @dlh
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 @pbiron
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.

Last edited 3 years ago by pbiron (previous) (diff)

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

3 years ago

#6 @davidbaumwald
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 @pbiron
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.

#8 @pbiron
3 years ago

@davidbaumwald what do you think? is this going to land today?

#9 @davidbaumwald
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 @pbiron
3 years ago


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 @davidbaumwald
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 @hellofromTonya
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 @dlh
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.

#15 @milana_cap
2 years ago

  • Keywords needs-docs added

This ticket was mentioned in PR #2527 on WordPress/wordpress-develop by dlh01.

2 years ago

  • Keywords has-patch has-unit-tests added; needs-unit-tests needs-patch removed

#17 @dlh
2 years ago

Well, it wasn't quite early, but a PR for these hooks is now available here:

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.

#18 @pbiron
2 years ago

I just made a few minor suggestions on the PR.

dlh01 commented on PR #2527:

2 years ago

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.

#20 @peterwilsoncc
2 years ago

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

In 53126:

Posts, Post Types/Taxonomies: Add object type specific registration filters.

Add post type and taxonomy specific registration argument hooks.

Introduces the filters register_{$post_type}_post_type_args and register_{$taxonomy}_taxonomy_args. Introduces the actions registered_post_type_{$post_type} and registered_taxonomy_{$taxonomy}.

Props pbiron, dlh, davidbaumwald, hellofromTonya, milana_cap.
Fixes #53212.

#23 @dlh
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:

pbiron commented on PR #2573:

2 years ago

Thanx David. LGTM as well!

#25 @peterwilsoncc
2 years ago

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

In 53170:

Docs: Improve doc blocks for hooks added in [53126].

Fix a variable name and provide examples of the hook names.

Follow up to [53126].

Props dlh, pbiron.
Fixes #53212.

peterwilsoncc commented on PR #2573:

2 years ago

Committed in, thanks for following this up.

Note: See TracTickets for help on using tickets.