我正在尝试清空使用设置API创建的选项,但失败。
update_option ( \'my_option\', \'\' );
似乎什么都不做delete_option ( \'my_option\' );
破坏整个选项,导致其他问题。我只想清空这些值并重置它。
我真的不知道如何正确地实现这一点。有人能帮忙吗?50分悬赏悬赏!
<?php
//Create the menus
add_action( \'admin_menu\', \'tccl_menu\' );
function tccl_menu() {
//add_menu_page: $page_title; $menu_title, $capability, $menu_slug, $function, $icon_url, $position
add_menu_page( \'Control\', \'Control\', \'manage_options\', \'tccl-main\', \'tccl_menu_page_main\', plugins_url( \'/traffic-control/images/wp-icon.png\' ), \'2.2\' );
//add_submenu_page: $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function
add_submenu_page( \'tccl-main\', \'Domains\', \'Domains\', \'manage_options\', \'tccl-domains\', \'tccl_menu_page_domains\' );
}
//Menu callback functions for drawing pages
function tccl_menu_page_main() {
?>
<div class="wrap">
<h2>Control</h2>
<form action="options.php" method="post">
<?php settings_fields( \'tccl_settings_main\' ); ?>
<?php do_settings_sections( \'tccl_settings_main\' ); ?>
<input name="Submit" type="submit" value="Save Changes" class="button-primary" />
</form></div>
<?php
}
function tccl_menu_page_domains() {
$tccl_domains = get_option( \'tccl_settings_domains\' );
if ( $_POST[\'trigger\'] ) {
$p_delete_all = $_POST[\'delete_all\'];
$p_ids = $_POST[\'ids\']; #Get IDs
$p_deletes = $_POST[\'deletes\']; #Get deletes
if ( $p_delete_all ) {
//unset( $tccl_domains );
//delete_option( \'tccl_settings_domains\' );
foreach( $tccl_domains as $option ) {
$option = false;
}
$tccl_domains = array ();
update_option( \'tccl_settings_domains\', $tccl_domains );
} elseif ( is_array( $p_ids) ){
foreach ( $p_ids as $id ) {
if ( $p_deletes[$id] ) {
//unset( $tccl_domains[$id] );
}
}
}
}
?>
<div class="wrap">
<?php screen_icon( \'themes\' ); ?>
<h2>Control</h2>
<form action="options.php" method="post">
<?php settings_fields( \'tccl_settings_domains\' ); ?>
<?php do_settings_sections( \'tccl_settings_domains\' ); ?>
<input name="Add" type="submit" value="Add Domains" class="button-primary" />
</form>
<form action="" method="post">
<input type="hidden" name="trigger" value="1">
<h3>Live Domains</h3>
<table class="widefat">
<thead>
<tr>
<th><input type="checkbox" name="delete_all" value="1"></th>
<th>Domain Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th><input type="checkbox" name="delete_all" value="1"></th>
<th>Domain Name</th>
</tr>
</tfoot>
<tbody>
<?php
print_r ( $tccl_domains );
if ( is_array( $tccl_domains ) ) {
foreach ( $tccl_domains as &$value ) {
echo \'<tr><td><input class="large-text" type="checkbox"></td><td>\'.$value[\'text_area\'].\'</td></tr>\';
}
} else {
echo \'<tr><td colspan="2">No domains entered. Use the form above to add domains to this list.</td></tr>\';
}
?>
</tbody>
</table>
<br />
<input name="Delete" type="submit" value="Delete Domains" class="button-secondary" />
</form>
<script>
jQuery(\'input[name=delete_all]\').click(function () {
if (jQuery(this).is(\':checked\')) {
jQuery(\'tbody input[type=checkbox]\').each(function () {
jQuery(this).attr(\'checked\', true);
});
jQuery(\'input[name=delete_all]\').attr(\'checked\', true);
} else {
jQuery(\'tbody input[type=checkbox]\').each(function () {
jQuery(this).attr(\'checked\', false);
});
jQuery(\'input[name=delete_all]\').attr(\'checked\', false);
}
});
</script>
</div>
<?php
}
//Settings
add_action( \'admin_init\', \'tccl_admin_init\' );
function tccl_admin_init() {
// register_setting: $option_group, $option_name, $sanitize_callback
register_setting( \'tccl_settings_main\', \'tccl_settings_main\', \'tccl_settings_main_validate\' );
register_setting( \'tccl_settings_domains\', \'tccl_settings_domains\', \'tccl_settings_domains_validate\' );
// add_settings_section: $id, $title, $callbak, $page
add_settings_section( \'tccl_sections_main\', \'Main Settings\', \'tccl_sections_main_text\', \'tccl_settings_main\' );
add_settings_section( \'tccl_sections_domains\', \'Add Domains\', \'tccl_sections_main_text\', \'tccl_settings_domains\' );
// add_settings_field: $id, $title, $callback, $page, $section, $args
add_settings_field( \'tccl_fields_main_input\', \'Enter text here...\', \'tccl_fields_main_input\', \'tccl_settings_main\', \'tccl_sections_main\' );
add_settings_field( \'tccl_fields_domains_input\', \'Add domains to the list below. Multiple domains should be seperated by commas.\', \'tccl_fields_domains_input\', \'tccl_settings_domains\', \'tccl_sections_domains\' );
}
//Sections callback functions
function tccl_sections_main_text() {
echo \'<p>Enter your settings for the main options in the main section...</p>\';
}
//Field callback functions
function tccl_fields_main_input() {
//Get option \'text_string\' value from the database
$options = get_option( \'tccl_settings_main\' );
$text_string = $options[\'text_string\'];
//Echo the field
echo "<input id=\'text_string\' name=\'tccl_settings_main[text_string]\' type=\'text\' value=\'$text_string\' />";
}
function tccl_fields_domains_input() {
//Get option \'text_string\' value from the database
$options = get_option( \'tccl_settings_domains\' );
$text_area = $options[\'text_area\'];
//Echo the field
echo "<textarea id=\'text_area\' name=\'tccl_settings_domains[text_area]\'>$text_area</textarea>";
}
//Settings callback functions (validation)
function tccl_settings_main_validate( $input ) {
$valid[\'text_string\'] = preg_replace( \'/[^a-zA-Z]/\', \'\', $input[\'text_string\'] );
if ( $valid[\'text_string\'] != $input[\'text_string\'] ) {
//add_setting_error: $title, $id, $error_message, $class
add_settings_error( \'tccl_fields_main_input\', \'tccl_texterror\', \'Incorrect value entered!\', \'error\' );
}
return $valid;
}
function tccl_settings_domains_validate( $input ) {
$options = get_option( \'tccl_settings_domains\' );
$options[] = $input;
return $options;
}
?>