The checked
function in WordPress is used to determine if a certain checkbox input is checked or not. It takes one argument, which is the current value of the checkbox input, and returns a boolean value indicating whether or not the checkbox is checked.
This function is useful when you want to perform certain actions based on whether or not a checkbox is checked. For example, if you have a form where a user can select multiple options, you can use the checked
function to determine which options have been selected and take appropriate actions based on those selections.
Here’s an example usage code:
<input type="checkbox" name="option1" value="1" <?php echo ( checked( $option1, '1', false ) ? 'checked' : '' ); ?> />
<label for="option1">Option 1</label>
In this example, we have a checkbox input with the name "option1" and value "1". We’re using the checked
function to determine if this checkbox is checked, and if it is, we’re adding the "checked" attribute to the input tag. If it’s not checked, we’re not adding the attribute.
So, this function can be used to output the correct HTML markup for a checkbox.