checked( mixed $checked, mixed $current = true, bool $echo = true )

Outputs the html checked attribute.


Description Description

Compares the first two arguments and if identical marks as checked


Parameters Parameters

$checked

(mixed) (Required) One of the values to compare

$current

(mixed) (Optional) (true) The other value to compare if not just true

Default value: true

$echo

(bool) (Optional) Whether to echo or just return the string

Default value: true


Top ↑

Return Return

(string) html attribute or empty string


Top ↑

Source Source

File: wp-includes/general-template.php

function checked( $checked, $current = true, $echo = true ) {
	return __checked_selected_helper( $checked, $current, $echo, 'checked' );
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    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 ); ?> />
    

You must log in before being able to contribute a note or feedback.