[wp-trac] [WordPress Trac] #39240: Add a generic return value function

WordPress Trac noreply at wordpress.org
Sun Dec 11 20:31:06 UTC 2016


#39240: Add a generic return value function
-------------------------+-----------------------------
 Reporter:  fjarrett     |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 This topic kicked off some
 [https://twitter.com/fjarrett/status/807829768745914368 healthy discussion
 on Twitter], so it seemed appropriate to just open a ticket for the idea
 to develop further.

 Since the second parameter in `add_filter()` can only accept a callback
 function, we've gotten used to having to go out of our way to define them.
 This was OK for a while, but has created some other baggage too, like more
 functions living in the global scope, people just lazily using the
 filter's tag as their callback name, which means increased chances of
 naming collisions, and not to mention just more lines of code in
 everything.

 I think it might be good to consider adding a generic return value
 function to core that would complement the existing `__return_*()` family.

 Since we will never be able to do what feels natural in these situations:

 {{{
 add_filter( 'twentyseventeen_front_page_sections', 6 );
 add_filter( 'site_icon_sizes', array( 256, 128, 64 ) );
 add_filter( 'rss_update_period', 'daily' );
 }}}


 Passing the values through a generic return wrapper could be a nice
 alternative:

 {{{
 add_filter( 'twentyseventeen_front_page_sections', __return( 6 ) );
 add_filter( 'site_icon_sizes', __return( array( 256, 128, 64 ) ) );
 add_filter( 'rss_update_period', __return( 'daily' ) );
 }}}

 Weston Ruter
 [https://gist.github.com/westonruter/35105d1e3d29a9964d9063894add3b40
 demonstrated how this could be done] while still maintaining 5.2
 compatibility, though it requires the use of `create_function()`.

 Maybe we can't take this seriously until 5.3 is on the table, at which
 point closures basically solve everything. But even so, I still kind of
 like the idea of a generic `__return()` since it's clean and fits nicely
 into a pattern we've already started.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39240>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list