I have created a WordPress plugin and added an options page.
There is an error message when the check box unchecked:
Notice: Undefined index: upo_enable
But when the check box checked it works good. See the GIF below:
The Demo Code:
<?php
/*
Plugin Name: update option
Plugin URI:
Description: Plugin Demo.
Author: Ryan
Version: 1.0
Author URI:
*/
if ( !function_exists('upo_admin_page') ) :
function upo_admin_page() {
add_options_page(
__( 'Update Option', 'demo' ),
__( 'Update Option', 'demo' ),
'manage_options',
'update-option',
'upo_option_page'
);
}
endif;
add_action( 'admin_menu', 'upo_admin_page' );
// Update Options.
if ( !function_exists('upo_options_update') ) :
function upo_options_update() {
$updated = '<div class="updated settings-error notice is-dismissible"><p><strong>' . __('Settings saved.', 'demo') . '</strong></p></div>';
if (isset($_POST['update_options'])) {
update_option('upo_enable', $_POST['upo_enable']);
echo $updated;
}
}
endif;
if ( !function_exists('upo_option_page') ) :
function upo_option_page() {
?>
<div class="upo-wrap">
<h2><?php _e('Update Options') ?></h2>
<?php upo_options_update(); ?>
<div class="update-option">
<form method="post" action="<?php echo admin_url( 'options-general.php?page=update-option' ); ?>">
<?php wp_nonce_field('update_options');?>
<table class="upo-options-form">
<tr valign="top">
<th scope="row"><?php _e('Enable'); ?></th>
<td><label for="upo_enable">
<input name="upo_enable" type="checkbox" id="upo_enable" value="true" <?php checked('true', get_option('upo_enable')); ?> /><?php _e('Test Text'); ?></label>
</td>
</tr>
</table>
<p class="upo-submit">
<input type="submit" name="update_options" class="button-primary" value="<?php _e('Save Changes','demo'); ?>" />
</p>
</form>
</div>
</div>
<?php
}
endif;
So, How can I fix it?
Thanks!