Make WordPress Core

Opened 6 years ago

Last modified 4 years ago

#43740 new enhancement

Filter WP_Post methods (vs. removing final/'get_post' filter)

Reported by: mikeschinkel's profile MikeSchinkel Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch dev-feedback needs-testing
Focuses: Cc:


There has been a lot of demand to remove final from WP_Post and to add a get_post filter, for good reason. There as also be a lot of pushback on those actions, also for very good reason. Both sets of reasons can be found in ticket #24672 and #12955, respectively.

In a nutshell for, there are many valid use-cases where being able to extend WP_Post would make for more robust and maintainable code. In the against case, removing 'final' could easily resolve in an explosion of incompatible child classes making plugin interoperability challenging, and backward compatibility almost impossible if code decided to evolve WP_Post in any way. And the 'get_post' filter would be almost as bad as removing final, and I am one who advocated for both for years.

However, we actually already have an extensibility mechanism that has shown the test of time and that is the use of filter hooks. Given a judicious use of filter hooks we could actually allow developers to extend WP_Post in a compossible manner thus minimizing plugin integration problems and also safeguarding core's ability to add enhancements in the future.

The proposal is basically to add filter hooks in the methods of WP_Post, and to add a __set() and __call() magic method as a companion to the existing __get() magic method. Here are the filters I am proposing:

Filter HookMethod
'wp_post_supports_instance' __construct()
'pre_wp_post__isset' __isset()
'wp_post__isset' __isset()
'wp_post__call' __call()
'wp_post__set' __set()
'pre_wp_post__get' __get()
'wp_post_filter' filter()
'wp_post_to_array' to_array()

And then this action hook:

  • 'wp_post__construct' in __construct()

I have attached a patch for class-wp-post.php that would implement these hooks.

Attachments (1)

43740.diff (5.0 KB) - added by MikeSchinkel 6 years ago.
Patch that would all method filters to WP_Post

Download all attachments as: .zip

Change History (5)

6 years ago

Patch that would all method filters to WP_Post

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

6 years ago

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

6 years ago

#3 @MikeSchinkel
6 years ago

We are potentially finding consensus on #12955 but that does not address built-in post types and I think this proposal is useful for built in post types.

#4 @Mte90
4 years ago

Looking this as today I think that implement for builtin post type is a good first step to see all the others problems/questions to handle.

Note: See TracTickets for help on using tickets.