Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Send meta_key to sanitize_callback functions #2

tollmanz opened this Issue · 6 comments

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.


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

@kovshenin kovshenin referenced this issue from a commit
@kovshenin added sanitize_callback_args that are passed on to the sanitize callb…
…ack function if not empty, related #2

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


@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.


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

@tollmanz tollmanz closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.