您不应该在HTML名称的索引周围加引号,否则最终会导致\'single_images\'
(包括单引号)作为PHP名称(因此必须对其进行寻址$iwmp_options["\'single_images\'"]
, 有些人可能会觉得困惑)。。。例如:
add_action( \'admin_init\', function () {
register_setting( \'iwmp_settings\', \'iwmp_settings\' );
add_settings_section( \'single_images\', __( \'Single Images\', \'iwmp_domain\' ) , function () {
$iwmp_options = get_option( \'iwmp_settings\' );
// Note quotes around the PHP index, no quotes around the HTML index.
// (Also id\'s should basically be alphanumeric with underscores/hyphens (for pre-HTML5 compatibility anyway).)
?>
<input id="iwmp_settings_single_images_" name="iwmp_settings[single_images]" type="checkbox" <?php checked( isset( $iwmp_options[\'single_images\'] ) ); ?> />
<label class="description" for="iwmp_settings_single_images_"><?php _e(\'Use with Single Images\', \'iwmp_domain\'); ?></label>
<br />
<input id="iwmp_settings_single_images_zoom_" name="iwmp_settings[single_images_zoom]" type="checkbox" <?php checked( isset( $iwmp_options[\'single_images_zoom\'] ) ); ?> />
<label class="description" for="iwmp_settings_single_images_zoom_"><?php _e(\'Use Zoom effect with Single Images\', \'iwmp_domain\'); ?></label>
<?php
}, \'iwmp_settings\' );
} );
add_action( \'admin_menu\', function () {
add_options_page( __( \'IWMP Settings\', \'iwmp_domain\' ), __( \'IWMP\', \'iwmp_domain\' ), \'manage_options\', \'iwmp_settings\', function() {
?>
<div class="wrap">
<h2><?php _e( \'IWMP Settings\', \'iwmp_domain\' ); ?></h2>
<form method="post" action="options.php">
<?php settings_fields( \'iwmp_settings\' ); ?>
<?php do_settings_sections( \'iwmp_settings\' ); ?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( \'Save Changes\', \'iwmp_domain\' ) ?>" />
</p>
</form>
</div>
<?php
} );
} );
还有,只是检查一下
isset()
在这里,复选框值使事情变得简单。如果你想操纵它们,那么设置一个变量,例如
$single_images = isset( $iwmp_options[\'single_images\'] );
或使用包装函数。(作为对短代码兼容性的补充说明,我发现
1/0
那么更方便
true/false
.)