checked()
Used in checkbox and radio fields of the form. Made for convenience, checks two values and if they match, outputs the attribute checked='checked' for the current form field.
This is one of the helper functions for forms: checked(), disabled(), selected(), wp_readonly().
There is a space at the beginning of the line: " checked='checked'".
When comparing values, they are first cast to the same type (string), and then compared:
(string) $one === (string) $two
Thus, a non-exact match will still trigger, for example:
- int
1will be equal to string'1'. truewill be equal to1or'1'.falsewill be equal to0or'0'.- etc.
No Hooks.
Returns
String. Outputs to the screen:
checked='checked'— when the passed values are equal to each other''(empty string) — when the passed values are not equal.
If the third parameter ($echo) is set to false, the function will not output anything to the screen.
Usage
<?php checked( $checked, $current, $echo ); ?>
- $checked(string/int/bool) (required)
- First of the values being compared.
- $current(string/int/bool)
- Second of the values being compared. Not required to fill, you just need to check the presence of the first parameter.
Default: true - $echo(bool)
- Output the result to the screen (true) or return for processing (false).
Default: true
Examples
#1 Demo
See what the function outputs depending on the input of different parameters.
checked( 1 ); // checked='checked' checked( 0 ); // '' checked( '1' ); // checked='checked' checked( '0' ); // '' checked( true ); // checked='checked' checked( false ); // '' checked( false, false ); // checked='checked' checked( 1, '1' ); // checked='checked' checked( true, '1' ); // checked='checked' checked( true, 'str' ); // '' checked( 2 ); // '' checked( 'str' ); // '' checked( array() ); // '' checked( array(true) ); // '' checked( array(1) ); // ''
#2 Example of using checked() in code
<?php
// get the option from the database. The option value is an array.
$option = get_option('slug_option');
// Get the value of the option we need
$checked = $option['self-destruct'] ?? false; // either true or 1 is expected here
?>
<input name="slug_option[self-destruct]" value="1"<?php checked( $checked ); ?>/> #3 Compare the PHP variant vs ``checked()
Option with `checked()
<input type="checkbox" name="options[postlink]" value="1"<?php checked( $options['postlink'] ); ?> >
Option with if()
<input type='checkbox' name='options[postlink]' value='1' <?php if ( 1 == $options['postlink'] ) echo 'checked="checked"'; ?> >
#4 Multiselect checkbox
For multiselect checkbox use array in checked function instead of simple value.
<?php $postlink = get_post_meta( $post->ID, 'postlink', true ); ?> <input type="checkbox" name="postlink[]" value="1" <?php checked( in_array( $postlink, 1 ), 1 ) ?> /> <input type="checkbox" name="postlink[]" value="2" <?php checked( in_array( $postlink, 2 ), 1 ) ?> /> <input type="checkbox" name="postlink[]" value="3" <?php checked( in_array( $postlink, 3 ), 1 ) ?> />
Changelog
| Since 1.0.0 | Introduced. |
checked() checked code WP 6.9.1
function checked( $checked, $current = true, $display = true ) {
return __checked_selected_helper( $checked, $current, $display, 'checked' );
}