最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress check box unchecked on null value ternary operator [plugin development]

programmeradmin0浏览0评论

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 ); ?>>
发布评论

评论列表(0)

  1. 暂无评论