1

I have a php form that I want to change the state of a checkbox so it's filled by default, but there are two fields that may be the right way:

'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',
            ),

I would have guessed that it would be 'default', but it's set to the string 'go' so perhaps I should set clear to false.

What's the correct way to make the checkbox marked by default? Any insight is appreciated.

  • Of course you have tried TRUE and 'checked' already, have you? – leymannx Apr 17 '19 at 19:16
  • checked works in the html but I need to change it in the php file instead. Where would you recommend I add TRUE? – James Apr 17 '19 at 19:21
  • 1
    'default' => TRUE. In Drupal it's 'default_value' => TRUE. And remove 'clear' => TRUE'. – leymannx Apr 17 '19 at 19:41
  • Yes it is definitively 'default' => true or 'default' => '1' – LoicTheAztec Apr 18 '19 at 0:32
  • 1
    Don't edit plugin files. You'll lose your changes if/when it's updated. Check the plugin docs or contact its author to find out the supported way to change this behaviour. – Jacob Peattie Apr 18 '19 at 7:06
0

It most likely must is '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,
]

If not, also try 'default_value' => TRUE, or try it without 'clear'.

Depending on what you want to achieve you might also want to get the default value dynamically. So that it restores the user's choice. Maybe get_option($option, $default) is the right choice here (if that's an Options API form).

'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),
]

or

'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,
]
| improve this answer | |

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.