WordPress.org

Make WordPress Core

Opened 9 years ago

Last modified 4 months ago

#20439 reopened enhancement

Introduce __return_one function

Reported by: bananastalktome Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: close
Focuses: Cc:

Description

Following #20357 and #18845 , I feel it would be useful to have a return function for 1 for use with filters.

Attachments (1)

patch.diff (616 bytes) - added by bananastalktome 9 years ago.

Download all attachments as: .zip

Change History (9)

#1 follow-up: @scribu
9 years ago

Let's not get carried away with these.

I would argue that even __return_zero() isn't common enough.

#2 @bananastalktome
9 years ago

I've had to create my own __return_zero() in order to set defaults (which cannot be overwritten by users) with filters pre_option_default_pingback_flag, pre_option_enable_xmlrpc, pre_option_use_trackback, etc., but I could just as easily see someone wanting to force open certain options as well with a __return_one(), which is why I decided to add this patch. Many of the options listed at http://codex.wordpress.org/Option_Reference use 0 or 1 as their value, so quick returns for their filter would be at least useful to some.

#3 @nacin
9 years ago

We use 1 and 0 because it is easy to store in a DB field. In reality you can simply use __return_true and __return_false.

#4 @nacin
9 years ago

It should be noted that will work for most situations. blog_public and a few other options could hypothetically have meaningful values > 1.

#5 in reply to: ↑ 1 @nacin
8 years ago

Replying to scribu:

I would argue that even __return_zero() isn't common enough.

I just had a pretty good use case for __return_zero() — I wanted to hook into pre_option_{$option} and turn an option off. Normally I would use __return_false but that is the value passed in that needs to be changed for the bypass to work (rather than null). So 0 it is.

#6 @nacin
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to maybelater
  • Status changed from new to closed

#7 @pcfreak30
4 months ago

  • Resolution maybelater deleted
  • Status changed from closed to reopened
  • Version set to trunk

I am aware this is old but I have lost track of the number of times I had to create a dummy function for this purpose when the calling code does a typecheck (true !== 1).

So I vote for this to be added as its simple and will get a lot of use I feel. as for __return_zero, I have used this plenty of times as well.

#8 @johnbillion
4 months ago

  • Keywords close added
  • Milestone set to Awaiting Review
  • Version trunk deleted

See the comment thread on #39240 for alternative approaches. Personally I don't think there's much demand for this as a dedicated function.

Note: See TracTickets for help on using tickets.