Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Send meta_key to sanitize_callback functions #2

tollmanz opened this Issue Aug 26, 2011 · 6 comments


None yet
2 participants

In order to do more advanced data processing, I think it would be help to pass the "meta_key" to the "sanitize_callback". Sure, you could define a new function for each field, which would allow you to know the "meta_key" of the value you are validating; however, in the case that you are using a single function for sanitizing different fields, the function is not aware of the previous value of the field.

My use case is that I would like to use the old field value in the event that the newly entered data fails validation. Currently, I cannot do this. Instead, I can really only return a placeholder or blank value.

I think that this can be patched by changing line 304 from:

$value = call_user_func( $option['callback']['sanitize_callback'], $value );


$value = call_user_func( $option['callback']['sanitize_callback'], $value, $option_id );

I've not tested this yet and am not sure that the "$option_id" is the correct value.

After our conversation and a bit of a think, I have a new suggestion. What if you make the field method in the "Post_Options_Fields_1_0" class accept a "fallback" or "default" parameter? The user can define, at the time of calling the "Post_Options_Fields_1_0" method what the value should be if the validation is failed. Then, in the big loop in "_save_post", you can check for an empty string (because if failed validation) and give it the fallback value.


kovshenin commented Aug 27, 2011

What if the input value was intended to be an empty string? :)

kovshenin added a commit that referenced this issue Aug 27, 2011

This looks really good so far. I'm going to do some testing of this and I'll give you some feedback.


kovshenin commented Aug 27, 2011

@tollmanz, great, thanks a lot!

@kovshenin, finally got this up and running. I was able to successfully set up callback functions that would fallback to previous values. The functions are reusable and do the appropriate validation and sanitization.

While I generally like this solution, I mourn the loss of the simplicity of using build in WordPress functions without writing an intermediary function. Probably can't have it both ways.

tollmanz commented Sep 2, 2011

I'm closing this because it has definitely been addressed.

@tollmanz tollmanz closed this Sep 2, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment