我创建了一个子菜单来为我的插件添加一些选项字段。据我所知,我已经遵循了文档,并且检查了我可以找到的有关该主题的每条论坛消息。
子菜单已按预期添加到“仪表板”菜单。
选择后,设置页将按预期显示,但是当我填写字段并按Save Changes时,新记录将添加到wp_options表中,但不会保存值。如果我使用SQL手动将值添加到数据库中,则该值显示在字段中,但是如果更改了该值,则不会保存新值。
这是我的代码
<?php // Zuzz Plugin Settings
if (!defined('ABSPATH')) die();
add_action( 'admin_menu', 'create_plugin_settings_page' );
function create_plugin_settings_page() {
// Add the menu item and page
$page_title = 'Zuzz Settings Page';
$menu_title = 'Zuzz Settings';
$capability = 'manage_options';
$slug = 'zuzz_settings_page';
$callback = 'plugin_settings_page_content';
$icon = 'dashicons-admin-plugins';
$position = 100;
add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $slug,$callback );
}
function plugin_settings_page_content() {
?>
<div class="wrap">
<h2>Zuzz Settings</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'zuzz_settings_page' );
if ( is_plugin_active( 'zuzz-mailing/zuzz-mailing.php' ) ) {
settings_fields( 'zuzz_settings_page' );
}
if ( is_plugin_active( 'zuzz-register/zuzz-register.php' ) ) {
settings_fields( 'zuzz_settings_page' );
}
do_settings_sections( 'zuzz_settings_page' );
submit_button();
?>
</form>
</div> <?php
}
add_action( 'admin_init','setup_sections');
function setup_sections() {
add_settings_section( 'general_settings', 'General settings', false, 'zuzz_settings_page' );
if ( is_plugin_active( 'zuzz-mailing/zuzz-mailing.php' ) ) {
add_settings_section( 'mailing', 'Mailing', false, 'zuzz_settings_page' );
}
if ( is_plugin_active( 'zuzz-register/zuzz-register.php' ) ) {
add_settings_section( 'register', 'Register', false, 'zuzz_settings_page' );
}
}
add_action( 'admin_init','setup_fields');
function setup_fields() {
add_settings_field( 'zuzz_club_name', 'Club name', 'field_callback', 'zuzz_settings_page', 'general_settings' , 'zuzz_club_name');
if ( is_plugin_active( 'zuzz-mailing/zuzz-mailing.php' ) ) {
add_settings_field( 'zuzz_create_post', 'Create post', 'field_callback', 'zuzz_settings_page', 'mailing', 'zuzz_create_post' );
add_settings_field( 'zuzz_region_coord', 'Coordinator email', 'field_callback', 'zuzz_settings_page', 'mailing', 'zuzz_region_coord' );
}
if ( is_plugin_active( 'zuzz-register/zuzz-register.php' ) ) {
add_settings_field( 'zuzz_super_registrar', 'Super registrar email', 'field_callback', 'zuzz_settings_page', 'register', 'zuzz_super_register' );
}
}
function field_callback( $arguments ) {
switch( $arguments ){
case 'zuzz_club_name':
echo '<input name="zuzz_club_name" id="zuzz_club_name" type="text" value="' . get_option( 'zuzz_club_name' ) . '" />';
register_setting( 'zuzz_settings_page', 'zuzz_club_name', 'sanitise_field' );
break;
case 'zuzz_create_post':
echo '<input name="zuzz_create_post" id="zuzz_create_post" type="checkbox" value="1"'. checked( "1", get_option( "zuzz_create_post" ), false ) . ' />';
register_setting( 'zuzz_settings_page', 'zuzz_create_post', 'sanitise_field' );
break;
case 'zuzz_super_register':
echo '<input name="zuzz_super_register" id="zuzz_super_register" type="email" value="' . get_option( 'zuzz_super_register' ) . '" />';
register_setting( 'zuzz_settings_page', 'zuzz_super_registrar', 'sanitise_field' );
break;
case 'zuzz_region_coord':
echo '<input name="zuzz_region_coord" id="zuzz_region_coord" type="email" value="' . get_option( 'zuzz_region_coord' ) . '" />';
register_setting( 'zuzz_settings_page', 'zuzz_region_coord', 'sanitise_field' );
break;
}
}
function sanitise_field($input) {
error_log($input);
return $input;
}