问题描述
我正在尝试清空使用 「设置」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;
}
?>
最佳解决方案
如果您正确编写代码,那么 delete_option 将是正确的方法。问题不是如何清除选项; 问题是如何构建您的代码,使 「选项不存在」 的情况是有效的情况。
想想吧第一次启动这个代码,那个选项根本就不会存在,对吧?您的代码应该完全有能力处理这种情况,因为这是用户第一次看到。
如果该选项不存在,则 get_option() 接受默认值。所以使用。例如,如果你有一个空数组,那么你可以使用这样的代码:
$options = get_option('whatever',array());
假设您正在使用设置 API,则应使用 if 语句中的 isset
函数来解释 missing-field 的情况。这样的东西
if (!isset($options['name'])) {
//... the option isn't set to something
} else {
//... the option is set to something
}
并且相应地处理每个实际使用该选项的情况。
次佳解决方案
你想取消只设置一个选项,还是想取消设置数组中的每个选项?
如果是前者:
$tccl_domains = get_option( 'tccl_settings_domains' );
$tccl_domains['option_to_unset'] = false;
update_option( 'tccl_settings_domains', $tccl_domains );
如果是后者:
$tccl_domains = get_option( 'tccl_settings_domains' );
foreach( $tccl_domains as $option ) {
$option = false;
}
update_option( 'tccl_settings_domains', $tccl_domains );
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。