我创建了一个WordPress插件并添加了一个选项页面。
复选框未选中时会显示错误消息:
Notice: Undefined index: upo_enable
但当复选框选中时,效果良好。请参见下面的GIF:演示代码:<?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;
那么,我该如何修复它呢?谢谢