我应该编辑哪个字段以使复选框在默认情况下被标记?

时间:2019-04-17 作者:James

我有一个php表单,我想更改复选框的状态,以便在默认情况下填充它,但有两个字段可能是正确的方式:

\'request_vendor_access\' => array(
                \'label\'    => __( \'Request access to become an Artist\', \'ignitewoo_vendor_stores\' ),
                \'type\'        => \'checkbox\',
                \'required\' => false,
                \'class\'    => array( \'form-row-full vendor_stores_request_access\' ),
                \'clear\'    => true,
                \'default\'    => \'go\',
            ),
我会猜到它会是“default”,但它被设置为字符串“go”,所以也许我应该设置clearfalse.

默认情况下标记复选框的正确方法是什么?任何见解都将不胜感激。

1 个回复
SO网友:leymannx

很可能是\'default\' => TRUE.

\'request_vendor_access\' => [
  \'label\'    => __(\'Request access to become an Artist\', \'ignitewoo_vendor_stores\'),
  \'type\'     => \'checkbox\',
  \'required\' => FALSE,
  \'class\'    => [\'form-row-full vendor_stores_request_access\'],
  \'clear\'    => TRUE,
  \'default\'  => TRUE,
]
如果没有,也请尝试\'default_value\' => TRUE, 或者不用\'clear\'.

根据您想要实现的目标,您可能还希望动态获取默认值。以便恢复用户的选择。大概get_option($option, $default) 是这里的正确选择(如果这是选项API表单)。

\'request_vendor_access\' => [
  \'label\'    => __(\'Request access to become an Artist\', \'ignitewoo_vendor_stores\'),
  \'type\'     => \'checkbox\',
  \'required\' => FALSE,
  \'class\'    => [\'form-row-full vendor_stores_request_access\'],
  \'clear\'    => TRUE,
  \'default\'  => get_option(\'request_vendor_access\', TRUE),
]

\'request_vendor_access\' => [
  \'label\'    => __(\'Request access to become an Artist\', \'ignitewoo_vendor_stores\'),
  \'type\'     => \'checkbox\',
  \'required\' => FALSE,
  \'class\'    => [\'form-row-full vendor_stores_request_access\'],
  \'clear\'    => TRUE,
  \'default\'  => isset(get_option(\'my_option\')[\'request_vendor_access\'] ? get_option(\'my_option\')[\'request_vendor_access\'] : TRUE,
]