add_action( \'admin_menu\', \'fhwtest_plugin_menu\' );
function fhwtest_plugin_menu() {
add_menu_page( \'Reservierungs-Einstellungen\', \'Reservierung\', \'administrator\', \'fhwtest_reservoptions\', \'fhwtest_plugin_options_seite\' );
add_action( \'admin_init\', \'fhwtest_plugin_options\' );
}
function fhwtest_plugin_options() {
register_setting( \'fhwtest_settings_group1\', \'fhwtest_bahnzahl\' );
register_setting( \'fhwtest_settings_group1\', \'fhwtest_email\' );
register_setting( \'fhwtest_settings_group1\', \'fhwtest_datenbankname\' );
}
function fhwtest_plugin_options_seite() {
if ( !is_admin() ) {
wp_die( __( \'Keine Berechtigung\' ) );
}
?>
<style>
fieldset {
border: 1px solid black;
padding: 15px;
}
legend { font-weight: bold; }
</style>
<div class="wrap">
<h1>Reservierungs-Einstellungen</h1>
<form method="post" action="options.php">
<?php settings_fields( \'fhwtest_settings_group1\' ); ?>
<?php do_settings_sections( \'fhwtest_settings_group1\' ); ?>
<fieldset>
<legend>Bahnen</legend>
<label>Anzahl Bahnen:
<input type="number" value="<?php echo esc_attr( get_option( \'fhwtest_bahnzahl\' ) ); ?>" name="bahnzahl" />
</label>
</fieldset>
<fieldset>
<legend>Benachrichtigung</legend>
<label>E-Mail an
<input name="email" value="<?php echo esc_attr( get_option( \'fhwtest_email\' ) ); ?>" />
</label>
</fieldset>
<fieldset>
<legend>Datenbank</legend>
<label>Datenbankname
<input name="email" value="<?php echo esc_attr( get_option( \'fhwtest_datenbankname\' ) ); ?>" />
</label>
</fieldset>
<?php submit_button(); ?>
</div>
<?php
}
?>
该站点是正确的,但如果我键入值并保存它,所有字段将再次为空。为什么他们没有得救?选项网站在提交后设置空字段
1 个回复
最合适的回答,由SO网友:Morgan Estes 整理而成
代码中需要注意的几点:
两个输入共享相同的“电子邮件”名称,因此当这些值在表单提交中发送时,第二个输入将覆盖第一个输入。
输入name
属性值应与要保存的选项的名称匹配。使用“fhwtest\\U email”代替“email”。否则,这些值将无法保存到正确的选项中。
get_option()
将返回false
如果没有为请求的选项名称设置值;如果你通过false
到esc_attr()
它将返回一个空字符串,因此下次保存时会将该值设置为空字符串。尽管有很多方法可以创建设置,但我建议看一下Plugin Developer\'s Handbook section on Settings 获取设置和选项API中的示例代码和详细信息。
结束