admin管理员组

文章数量:1429658

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

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 ); ?> />
Share Improve this question edited May 22, 2019 at 7:29 nmr 4,5672 gold badges17 silver badges25 bronze badges asked May 22, 2019 at 7:20 ApsaraArunaApsaraAruna 156 bronze badges 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 Commented May 22, 2019 at 14:00
Add a comment  | 

1 Answer 1

Reset to default 0

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.

<?php 
  global $options;
  $enabled = ( isset( $options['enable'] ) ) ? $options['enable']: '';
?>
<input name="settings['enable']" type="checkbox" id="" value="1" <?php checked( $enabled, 1 ); ?>>

本文标签: Wordpress check box unchecked on null value ternary operator plugin development