Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

checkbox default 'on' not working #71

Open
alekseyn1 opened this issue Feb 16, 2019 · 2 comments
Open

checkbox default 'on' not working #71

alekseyn1 opened this issue Feb 16, 2019 · 2 comments

Comments

@alekseyn1
Copy link

Hello,
checkbox default 'on' not working

when settings screen gets loaded the first time after plugin activation, the checkbox is unchecked.

I am on the latest WP (5.0.3) and PHP 7.2

Any help would be greatly appreciated

				array(  //A standard checkbox - if you save this option as checked then it will store the option as \'on\', otherwise it will be an empty string.
					'id' 			=> 'enable_email_notification_checkbox',
					'label'			=> __( 'E-mail notification', 'test_plugin_text' ),
					'description'		=> __( 'Default is on. Select this option to enable a welcome email notification to new users created by this plugin. ', 'test_plugin_text' ),
					'type'			=> 'checkbox',
					'default'		=> 'on'
@alekseyn1
Copy link
Author

Here is a working solution.
Replace the entire case: entry with this

			case 'checkbox':
				$checked = '';
				
			       global $wpdb;
			       $empty_option = false;
			       $row = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option_name));
			       if (is_object($row)) {
			           $empty_option = true;
			       }
				
				if( ($option) || ( $empty_option == false && $field['default'] ) ){
					$checked = 'checked="checked"';
				}
				$html .= '<input id="' . esc_attr( $field['id'] ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" ' . $checked . '/>' . "\n";
			break;

@jonathanbossenger
Copy link
Collaborator

Hey @alekseyn1 thanks for this.

Are you able to open a pull request with these changes. I can't guarantee I will be able to merge it very soon, but if it's open I can at least try and prioritise it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants