WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/checked

Description

For use in checkbox and radio button form fields. Compares two given values (for example, a saved option vs. one chosen in a form) and, if the values are the same, adds the checked attribute to the current radio button or checkbox.

Usage

 <?php checked$checked$current$echo ); ?> 

Parameters

$checked
(mixed) (required) One of the values to compare.
Default: None
$current
(mixed) (optional) The other value to compare if not just true.
Default: true
$echo
(boolean) (optional) Whether to echo or just return the string.
Default: true

Returns

(string) 
HTML attribute (checked='checked') or empty string.

Example

<?php

// Get an array of options from the database.
$options = get_option( 'slug_option' );

// Get the value of this option.
$checked = $options['self-destruct'];

// The value to compare with (the value of the checkbox below).
$current = 1; 

// True by default, just here to make things clear.
$echo = true;

?>
<input name="slug-option[self-destruct]" value="1" <?php checked( $checked, $current, $echo ); ?>/>

Testing the value with if():

<input type='checkbox' name='options[postlink]' value='1' <?php if ( 1 == $options['postlink'] ) echo 'checked="checked"'; ?> />

Using checked() instead:

<input type="checkbox" name="options[postlink]" value="1" <?php checked( $options['postlink'], 1 ); ?> />

Changelog

Since: 1.0

Source File

checked() is located in wp-includes/general-template.php.

Related

selected(), disabled()