Using WordPress ‘checked()’ PHP function

The checked() WordPress PHP function outputs the HTML checked attribute. This function compares the first two arguments and if they are identical, it marks them as checked.


Let’s say we have an option in our WordPress settings called ‘enable_feature’, and we want to create a checkbox for it. We can use the checked() function to automatically check this box if the option is currently enabled.

<input type="checkbox" name="enable_feature" value="1" 
    <?php checked( get_option('enable_feature'), 1 ); ?> />

In the above code, checked() compares the value of ‘enable_feature’ option with ‘1’. If they are the same, it outputs the checked attribute, marking the checkbox as checked.


  • $checked (mixed): One of the values to compare. This is required.
  • $current (mixed): The other value to compare. This is optional and defaults to ‘true’.
  • $echo (bool): Whether to echo or just return the string. This is optional and defaults to ‘true’.

More information

See WordPress Developer Resources: checked()


Basic checkbox

This code creates a simple checkbox which gets checked if ‘option_name’ is set to ‘1’.

<input type="checkbox" name="option_name" value="1"
    <?php checked( get_option('option_name'), 1 ); ?> />

Checking for option existence

Sometimes options might not be set, and we need to account for that. This example checks if ‘option_name’ is set before comparing its value.

$options = get_option('option_name');
if(!isset($options)) {
    $options = 0;
<input type="checkbox" name="option_name" value="1" 
    <?php checked( $options, 1 ); ?> />

Using with a form

Here we’re using checked() within a form to remember user’s choice.

<form method="post">
    <input type="checkbox" name="remember_me" value="1" 
        <?php checked( $_POST['remember_me'], 1 ); ?> /> Remember Me

Multiple checkboxes

This example shows how to use checked() function with multiple checkboxes.

$postlink = get_post_meta($post->ID, 'postlink', true);
<input type="checkbox" name="postlink" value="1" 
    <?php checked(in_array($postlink, 1), true); ?> />
<input type="checkbox" name="postlink" value="2" 
    <?php checked(in_array($postlink, 2), true); ?> />
<input type="checkbox" name="postlink" value="3" 
    <?php checked(in_array($postlink, 3), true); ?> />