我在保存插件设置方面花了很多时间。我已经检查了很多次代码,我不得不认为我只是错过了一些东西。我正在遵循设置API,但我在插件设置页面上所做的任何更改都不会保存。我错过了什么?
function __construct() {
self::$instance = $this;
// Add "Settings" link to plugin page
add_filter( \'plugin_action_links_\' . plugin_basename( __FILE__ ) , array( $this, \'settings_link\' ) );
// Creates the CEMB Seminar submenu
add_action( \'admin_menu\', array( $this, \'add_menu\' ) );
// Register and define the settings
add_action( \'admin_init\', array( $this, \'settings_init\' ) );
} // End of __construct()
/***** Menus *****/
// Add "Settings" link to plugin page
function settings_link( $links ) {
$settings_link = sprintf( \'<a href="%s">%s</a>\', admin_url( \'options-general.php?page=cemb-seminar\' ), __( \'Settings\' ) );
array_unshift( $links, $settings_link );
return $links;
}
// Creates the CEMB Seminar submenu
function add_menu() {
add_submenu_page(
\'edit.php?post_type=cemb_seminar\',
__( \'Course Requirements\' ),
__( \'Course Requirements\' ),
\'manage_options\',
\'cemb-seminar\',
array( $this, \'settings_page\' )
);
}
/***** Course Requirement Settings *****/
/* Creates new database fields */
function settings_init() {
$options = array(
\'cemb_seminar_seminars_college\' => \'0\',
\'cemb_seminar_showcases_college\' => \'0\',
\'cemb_seminar_writers_college\' > \'0\',
\'cemb_seminar_aet_college\' => \'0\',
\'cemb_seminar_mbu_college\' => \'0\',
\'cemb_seminar_eis_college\' => \'0\',
\'cemb_seminar_sng_college\' => \'0\',
);
update_option( \'cemb_seminar_options\', $options );
register_setting(
\'cemb_seminar_options\',
\'cemb_seminar_options\'
);
add_settings_section(
\'cemb_seminar_reqs_college\',
\'Course Requirements\',
array( $this, \'reqs_college_fn\' ),
\'cemb-seminar\'
);
add_settings_field(
\'cemb_seminar_seminars_college_field\',
\'Seminars\',
array( $this, \'seminars_college_fn\' ),
\'cemb-seminar\',
\'cemb_seminar_reqs_college\'
);
add_settings_field(
\'cemb_seminar_showcases_college_field\',
\'Showcases\',
array( $this, \'showcases_college_fn\' ),
\'cemb-seminar\',
\'cemb_seminar_reqs_college\'
);
add_settings_field(
\'cemb_seminar_writers_college_field\',
\'Writer\\\'s Nights\',
array( $this, \'writers_college_fn\' ),
\'cemb-seminar\',
\'cemb_seminar_reqs_college\'
);
add_settings_field(
\'cemb_seminar_aet_college_field\',
\'AET Seminars\',
array( $this, \'aet_college_fn\' ),
\'cemb-seminar\',
\'cemb_seminar_reqs_college\'
);
add_settings_field(
\'cemb_seminar_mbu_college_field\',
\'MBU Seminars\',
array( $this, \'mbu_college_fn\' ),
\'cemb-seminar\',
\'cemb_seminar_reqs_college\'
);
add_settings_field(
\'cemb_seminar_eis_college_field\',
\'EIS Seminars\',
array( $this, \'eis_college_fn\' ),
\'cemb-seminar\',
\'cemb_seminar_reqs_college\'
);
add_settings_field(
\'cemb_seminar_sng_college_field\',
\'SNG Seminars\',
array( $this, \'sng_college_fn\' ),
\'cemb-seminar\',
\'cemb_seminar_reqs_college\'
);
} // End of settings_init()
// Add note about the course requirements settings
function reqs_college_fn() {
echo \'<p>The course requirement options below will apply to all students for all majors.</p>\';
}
// Checks for the selected value in the drop menus
function course_req_selected( $selected, $option ) {
// Get options first
$options = get_option( \'cemb_seminar_options\' );
// Check if the option matches the input
if ( $options[\'$option\'] == $selected ) {
echo \' selected="selected"\';
}
} // End of course_req_selected()
// Add college-wide Seminars requirements Field
function seminars_college_fn() {
// Get options from database
$options = get_option( \'cemb_seminar_options\' );
$option = $options[\'cemb_seminar_seminars_college\'];
// Build the select form ?>
<select id="cemb_seminar_seminars_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_seminars_college]">
<option value="0" <?php if ( $option == \'0\' ) { echo \'selected="selected"\'; } ?>>0</option>
<option value="1" <?php if ( $option == \'1\' ) { echo \'selected="selected"\'; } ?>>1</option>
<option value="2" <?php if ( $option == \'2\' ) { echo \'selected="selected"\'; } ?>>2</option>
<option value="3" <?php if ( $option == \'3\' ) { echo \'selected="selected"\'; } ?>>3</option>
<option value="4" <?php if ( $option == \'4\' ) { echo \'selected="selected"\'; } ?>>4</option>
<option value="5" <?php if ( $option == \'5\' ) { echo \'selected="selected"\'; } ?>>5</option>
</select> <?php
} // End of seminars_college_fn()
// Add college-wide Showcases requirements Field
function showcases_college_fn() {
// Get options from database
$options = get_option( \'cemb_seminar_options\' );
$option = $options[\'cemb_seminar_showcases_college\'];
// Build the select form ?>
<select id="cemb_seminar_showcases_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_showcases_college]">
<option value="0" <?php if ( $option == \'0\' ) { echo \'selected="selected"\'; } ?>>0</option>
<option value="1" <?php if ( $option == \'1\' ) { echo \'selected="selected"\'; } ?>>1</option>
<option value="2" <?php if ( $option == \'2\' ) { echo \'selected="selected"\'; } ?>>2</option>
<option value="3" <?php if ( $option == \'3\' ) { echo \'selected="selected"\'; } ?>>3</option>
<option value="4" <?php if ( $option == \'4\' ) { echo \'selected="selected"\'; } ?>>4</option>
<option value="5" <?php if ( $option == \'5\' ) { echo \'selected="selected"\'; } ?>>5</option>
</select> <?php
} // End of showcases_college_fn()
// Add college-wide Writer\'s Night requirements field
function writers_college_fn() {
// Get options from database
$options = get_option( \'cemb_seminar_options\' );
$option = $options[\'cemb_seminar_writers_college\'];
// Build the select form */?>
<select id="cemb_seminar_writers_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_writers_college]">
<option value="0" <?php if ( $option == \'0\' ) { echo \'selected="selected"\'; } ?>>0</option>
<option value="1" <?php if ( $option == \'1\' ) { echo \'selected="selected"\'; } ?>>1</option>
<option value="2" <?php if ( $option == \'2\' ) { echo \'selected="selected"\'; } ?>>2</option>
<option value="3" <?php if ( $option == \'3\' ) { echo \'selected="selected"\'; } ?>>3</option>
<option value="4" <?php if ( $option == \'4\' ) { echo \'selected="selected"\'; } ?>>4</option>
<option value="5" <?php if ( $option == \'5\' ) { echo \'selected="selected"\'; } ?>>5</option>
</select> <?php
} // End of writers_college_fn()
// Add college-wide AET Seminars requirements field
function aet_college_fn() {
// Get options from database
$options = get_option( \'cemb_seminar_options\' );
$option = $options[\'cemb_seminar_aet_college\'];
// Build the select form ?>
<select id="cemb_seminar_aet_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_aet_college]">
<option value="0" <?php if ( $option == \'0\' ) { echo \'selected="selected"\'; } ?>>0</option>
<option value="1" <?php if ( $option == \'1\' ) { echo \'selected="selected"\'; } ?>>1</option>
<option value="2" <?php if ( $option == \'2\' ) { echo \'selected="selected"\'; } ?>>2</option>
<option value="3" <?php if ( $option == \'3\' ) { echo \'selected="selected"\'; } ?>>3</option>
<option value="4" <?php if ( $option == \'4\' ) { echo \'selected="selected"\'; } ?>>4</option>
<option value="5" <?php if ( $option == \'5\' ) { echo \'selected="selected"\'; } ?>>5</option>
</select> <?php
} // End of aet_college_fn()
// Add college-wide MBU Seminars requirements field
function mbu_college_fn() {
// Get options from database
$options = get_option( \'cemb_seminar_options\' );
$option = $options[\'cemb_seminar_mbu_college\'];
// Build the select form ?>
<select id="cemb_seminar_mbu_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_mbu_college]">
<option value="0" <?php if ( $option == \'0\' ) { echo \'selected="selected"\'; } ?>>0</option>
<option value="1" <?php if ( $option == \'1\' ) { echo \'selected="selected"\'; } ?>>1</option>
<option value="2" <?php if ( $option == \'2\' ) { echo \'selected="selected"\'; } ?>>2</option>
<option value="3" <?php if ( $option == \'3\' ) { echo \'selected="selected"\'; } ?>>3</option>
<option value="4" <?php if ( $option == \'4\' ) { echo \'selected="selected"\'; } ?>>4</option>
<option value="5" <?php if ( $option == \'5\' ) { echo \'selected="selected"\'; } ?>>5</option>
</select> <?php
} // End of mbu_college_fn()
// Add college-wide EIS Seminars requirements field
function eis_college_fn() {
// Get options from database
$options = get_option( \'cemb_seminar_options\' );
$option = $options[\'cemb_seminar_eis_college\'];
// Build the select form ?>
<select id="cemb_seminar_eis_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_eis_college]">
<option value="0" <?php if ( $option == \'0\' ) { echo \'selected="selected"\'; } ?>>0</option>
<option value="1" <?php if ( $option == \'1\' ) { echo \'selected="selected"\'; } ?>>1</option>
<option value="2" <?php if ( $option == \'2\' ) { echo \'selected="selected"\'; } ?>>2</option>
<option value="3" <?php if ( $option == \'3\' ) { echo \'selected="selected"\'; } ?>>3</option>
<option value="4" <?php if ( $option == \'4\' ) { echo \'selected="selected"\'; } ?>>4</option>
<option value="5" <?php if ( $option == \'5\' ) { echo \'selected="selected"\'; } ?>>5</option>
</select> <?php
} // End of eis_college_fn()
// Add college-wide SNG Seminars requirements field
function sng_college_fn() {
// Get options from database
$options = get_option( \'cemb_seminar_options\' );
$option = $options[\'cemb_seminar_sng_college\'];
// Build the select form ?>
<select id="cemb_seminar_sng_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_sng_college]">
<option value="0" <?php if ( $option == \'0\' ) { echo \'selected="selected"\'; } ?>>0</option>
<option value="1" <?php if ( $option == \'1\' ) { echo \'selected="selected"\'; } ?>>1</option>
<option value="2" <?php if ( $option == \'2\' ) { echo \'selected="selected"\'; } ?>>2</option>
<option value="3" <?php if ( $option == \'3\' ) { echo \'selected="selected"\'; } ?>>3</option>
<option value="4" <?php if ( $option == \'4\' ) { echo \'selected="selected"\'; } ?>>4</option>
<option value="5" <?php if ( $option == \'5\' ) { echo \'selected="selected"\'; } ?>>5</option>
</select> <?php
} // End of sng_college_fn()
// Creates the course requirements page
function settings_page() {
?>
<div class="wrap">
<div class="icon32" style="background-image:url(<?php echo WP_PLUGIN_URL . \'/cemb-seminar/images/cemb-logo32x32.png\'; ?>); background-repeat:no-repeat;"><br /></div>
<h2>Course Requirements</h2>
<form method="post" action="options.php">
<?php settings_fields( \'cemb_seminar_options\' ); ?>
<?php do_settings_sections( \'cemb-seminar\' ); ?>
<br />
<input class="button-primary" type="submit" name="Submit" value=" <?php _e( \'Save Settings\' ); ?> " />
</form>
</div> <?php
} // End of settings_page()
最合适的回答,由SO网友:Bainternet 整理而成
问题是您一直将值放在这里:
$options = array(
\'cemb_seminar_seminars_college\' => \'0\',
\'cemb_seminar_showcases_college\' => \'0\',
\'cemb_seminar_writers_college\' > \'0\',
\'cemb_seminar_aet_college\' => \'0\',
\'cemb_seminar_mbu_college\' => \'0\',
\'cemb_seminar_eis_college\' => \'0\',
\'cemb_seminar_sng_college\' => \'0\',
);
update_option( \'cemb_seminar_options\', $options );
将其更改为
$options = get_option(\'cemb_seminar_options\');
if ($options === false){
$options = array(
\'cemb_seminar_seminars_college\' => \'0\',
\'cemb_seminar_showcases_college\' => \'0\',
\'cemb_seminar_writers_college\' > \'0\',
\'cemb_seminar_aet_college\' => \'0\',
\'cemb_seminar_mbu_college\' => \'0\',
\'cemb_seminar_eis_college\' => \'0\',
\'cemb_seminar_sng_college\' => \'0\',
);
update_option( \'cemb_seminar_options\', $options );
}
因此,只有在数据库中未设置“cemb\\u seminar\\u options”时,它才会运行