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

Top ↑

Parameters Parameters


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


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

Default value: true


(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

Version Description
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex


    // 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 ); ?> />
  2. Skip to note 2 content
    Contributed by Beda

    Note that checking for


    may/is not enough!

    Options are not saved if left empty (in the checkbox case == unchecked).

    Thus, before checked( $options['option_name'], 1 ); we should also check if the option is actually set, with something like:

    $options = get_option( 'wpdocs_option_array' );
    if ( ! isset( $options['option'] ) ) {
        $options['option'] = 0;
  3. Skip to note 3 content
    Contributed by sandeepjainlive

    For multiselect checkbox use array in checked function instead of simple value.

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

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