I'm trying to use wordpress plugin development, without a checked checkbox getting an error.

<?php global $options; ?>
<input name="settings[enable]" type="checkbox" id="" 
    value="1" <?php checked( $options['enable'], 1 ); ?> />
  • Does print_r($options) give you anything? Depending on the context this may or may not be the way you need to refer to the options. Also, your input name needs to be settings['enable'] with quotes. – WebElaine May 22 '19 at 14:00

checked() only checks if the passed first and second parameters match. It doesn't do any array key checking, so you need to do it yourself before using the function to avoid errors.

  global $options;
  $enabled = ( isset( $options['enable'] ) ) ? $options['enable']: '';
<input name="settings['enable']" type="checkbox" id="" value="1" <?php checked( $enabled, 1 ); ?>>
| improve this answer | |
  • Thanks for the reply. This is work for me . <input name="settings[enable]" type="checkbox" id="" value="1" <?php if (isset($options['enable']) && (1 == $options['enable'])) echo 'checked="checked"'; ?> /> – Backslash May 22 '19 at 17:30
  • Yes, checking if the key exists and if the value is the correct one yourself is a another way to do the same thing. checked() basically does the (1 == $options['enable'])) echo 'checked="checked"' part of your code. – Antti Koskinen May 22 '19 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.