how to sanitize checkbox input?

asis Stha asked
metabox sanitization

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?


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:

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.

Share This
Posted in: