3

I've added meta box: checkbox on admin edit screen

<input type="checkbox" name="changeposition" />

So how could we sanitize the input came from the checkbox as we do for input type text by sanitize_text_field(). Is there any function like this for checkbox sanitization or should we create custom method for it?

2
  • 1
    You can use absint().
    – Minh Tri
    Commented May 9, 2016 at 7:34
  • 1
    I do not think you will need it. Text field value come from user input here in checkbox you are setting the value. However, value can be modified by HTML inspector, so you can use the same function sanitize_text_field().
    – Sumit
    Commented May 9, 2016 at 7:38

2 Answers 2

2

Be sure to set the value in your markup. You should have.

<input type="checkbox" name="changeposition" value="yes" />

Then, I'd suggest using sanitize_key() to sanitize.

Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.

Think of the word yes, as a key. That's what you're expecting is a lowercase alphanumeric value.


See also: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox

If the value attribute was omitted, the submitted data would be given a default value of on, so the submitted data in that case would be subscribe=on.

0

I would suggest the filter_var() function in PHP. It has some predefined filters thats you can use.

To sanitize a number:

$sanitizedCheckbox = filter_var( $yourVar, FILTER_SANITIZE_NUMBER_INT );

For a string you would just change _NUM_INT to _STRING.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.