admin管理员组

文章数量:1023070

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.

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.

Share Improve this question asked Apr 17, 2019 at 19:00 JamesJames 1114 bronze badges 5
  • Of course you have tried TRUE and 'checked' already, have you? – norman.lol Commented Apr 17, 2019 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 Commented Apr 17, 2019 at 19:21
  • 1 'default' => TRUE. In Drupal it's 'default_value' => TRUE. And remove 'clear' => TRUE'. – norman.lol Commented Apr 17, 2019 at 19:41
  • Yes it is definitively 'default' => true or 'default' => '1' – LoicTheAztec Commented Apr 18, 2019 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 Commented Apr 18, 2019 at 7:06
Add a comment  | 

1 Answer 1

Reset to default 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,
]

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.

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.

Share Improve this question asked Apr 17, 2019 at 19:00 JamesJames 1114 bronze badges 5
  • Of course you have tried TRUE and 'checked' already, have you? – norman.lol Commented Apr 17, 2019 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 Commented Apr 17, 2019 at 19:21
  • 1 'default' => TRUE. In Drupal it's 'default_value' => TRUE. And remove 'clear' => TRUE'. – norman.lol Commented Apr 17, 2019 at 19:41
  • Yes it is definitively 'default' => true or 'default' => '1' – LoicTheAztec Commented Apr 18, 2019 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 Commented Apr 18, 2019 at 7:06
Add a comment  | 

1 Answer 1

Reset to default 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,
]

本文标签: phpWhich field should I edit to make the checkbox marked by default