Make WordPress Core

Opened 14 years ago

Last modified 9 months ago

#15335 reopened defect (bug)

register_setting() filter for sanitization callback needs to indicate 2 arguments accepted

Reported by: lumination's profile lumination Owned by: markjaquith's profile markjaquith
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Options, Meta APIs Keywords: has-patch
Focuses: administration Cc:

Description

register_setting() adds the function for sanitization of the option with the following line (wp-admin/includes/plugin.php):

add_filter( "sanitize_option_{$option_name}", $sanitize_callback );

For users wanting to declare a function for option sanitization, use of the option name within the function is sometimes desired, and is provided for in the filter application inside the sanitize_option() declaration as such (wp-includes/formatting.php):

$value = apply_filters("sanitize_option_{$option}", $value, $option);

With the filter always being applied with the option name as the second argument, I see no reason not to propose that the add_filter() call in register_setting() include "2" as the number of accepted arguments, allowing the option name to be passed.

Attachments (4)

15335.diff (486 bytes) - added by solarissmoke 14 years ago.
15335.2.diff (479 bytes) - added by solarissmoke 14 years ago.
15335_params.diff (1.1 KB) - added by ejdanderson 13 years ago.
Filter args as optional parameters.
15335.patch (1.3 KB) - added by ocean90 6 years ago.

Download all attachments as: .zip

Change History (30)

#1 @goto10
14 years ago

  • Cc goto10 added

#2 @lumination
14 years ago

  • Component changed from General to Plugins

#3 @nacin
14 years ago

  • Keywords needs-patch added; register_setting removed
  • Milestone changed from Awaiting Review to Future Release

It's not difficult to derive this from current_filter(). Seems sane though. Future pending patch.

@solarissmoke
14 years ago

#4 @solarissmoke
14 years ago

  • Keywords has-patch added; needs-patch removed

#5 @nacin
14 years ago

This is an add_filter() call, not apply_filters(). So probably needs to be 10, 2.

#6 @solarissmoke
14 years ago

Oops, yeah, bit slow this morning :)

@ejdanderson
13 years ago

Filter args as optional parameters.

#7 @markjaquith
13 years ago

  • Keywords early added
  • Owner set to markjaquith
  • Status changed from new to accepted

This is a really lame omission. You should be able to have a generic save handler that does a switch on the option name and does your sanitization magic. 3.3-early, please.

+1 for solarissmoke's patch: 15335.2.diff

Last edited 13 years ago by markjaquith (previous) (diff)

#8 @SergeyBiryukov
13 years ago

  • Milestone changed from Future Release to 3.3

#9 @markjaquith
13 years ago

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

In [18887]:

Pass both parameters to the sanitize_option_FOO callback set in register_setting(). props lumination. fixes #15335

#10 @duck_
13 years ago

  • Milestone changed from 3.3 to Future Release
  • Resolution fixed deleted
  • Status changed from closed to reopened

[18951] reverted [18887], see #18914.

Need to deal with back compat when using intval, for example, as a callback. See also #14671.

#11 @toscho
11 years ago

  • Cc info@… added

#12 @nacin
11 years ago

#26084 was marked as a duplicate.

#13 @nacin
11 years ago

  • Component changed from Plugins to Admin APIs
  • Focuses administration added

#14 @nacin
11 years ago

  • Component changed from Admin APIs to Plugins

Sorry for the noise.

#15 @SergeyBiryukov
10 years ago

  • Component changed from Plugins to Options, Meta APIs

#16 @SergeyBiryukov
10 years ago

#29193 was marked as a duplicate.

#17 @chriscct7
9 years ago

  • Keywords needs-patch added; has-patch removed

#18 @ericlewis
9 years ago

  • Keywords close added; needs-patch early removed

@duck_ said

Need to deal with back compat when using intval, for example

Sounds like this is a deal breaker.

#19 @swissspidy
8 years ago

Sounds like #14671 would need to be fixed first, although it seems like the reflection thing is out of the question. Thus, this ticket here would become a wontfix.

#20 @peterwilsoncc
7 years ago

#43629 was marked as a duplicate.

#21 follow-up: @peterwilsoncc
7 years ago

Over in #43629, @seanleavey has suggested adding the priority and number of arguments accepted to the register_setting arguments. This could be a workaround for #14671.

This would require modification to unregister_setting removing the filter.

I'm undecided on the suitability of this solution.

#22 in reply to: ↑ 21 @seanleavey
7 years ago

Replying to peterwilsoncc:

Over in #43629, @seanleavey has suggested adding the priority and number of arguments accepted to the register_setting arguments. This could be a workaround for #14671.

This would require modification to unregister_setting removing the filter.

I'm undecided on the suitability of this solution.

It's not as nice as #14671's proposal, but it's better than nothing. Saying that, since WordPress 5.0 is round the corner, could this be the time to break backwards compatibility regarding intval as a filter? The message could just be: don't use intval and other functions that don't have a suitable signature for a WordPress sanitization filter.

@ocean90
6 years ago

#23 @ocean90
6 years ago

  • Keywords has-patch added; close removed

Since register_setting() now supports an $args argument, how about a sanitize_callback_accepted_args which defaults to 1 as seen in 15335.patch?

#24 @jlambe
6 years ago

I was going to report this as well and I found this original issue on trac.

Just to go against the title issue, I suggest that the call to the add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback']); (options.php - Line 2026 - WordPress 4.9.6) does define the 3 arguments provided by the apply_filters( "sanitize_option_{$option}", $value, $option, $original_value ); (formatting.php - Line 4314 - WordPress 4.9.6).

Indeed it is quite useful to get the setting name and it would make the code cleaner I suppose for some sanitization and validation on the server side.

+1 to finally get this one merged on next release please :)

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


12 months ago

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


9 months ago

Note: See TracTickets for help on using tickets.