WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 4 months ago

#53212 accepted enhancement

add post_type-specific version of the register_post_type_args filter

Reported by: pbiron Owned by: davidbaumwald
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch needs-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)

53212.diff (1.3 KB) - added by pbiron 5 months ago.

Download all attachments as: .zip

Change History (12)

@pbiron
5 months ago

#1 @pbiron
5 months ago

  • Keywords has-patch added

#2 @pbiron
5 months 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
5 months 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
5 months 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 5 months ago by pbiron (previous) (diff)

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


4 months ago

#6 @davidbaumwald
4 months 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
4 months 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
4 months ago

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

#9 @davidbaumwald
4 months 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
4 months 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 @davidbaumwald
4 months 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.

Note: See TracTickets for help on using tickets.