I’m trying to empty an option created with the Settings API and failing.
update_option ( 'my_option', '' );
appears to do nothing, whereas delete_option ( 'my_option' );
destroys the whole option causing other problems. I just want to empty the values and reset it.
I’m really clueless how to implement this correctly. Can anyone help? 50 point bounty up for grabs!
<?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;
}
?>
If you wrote your code correctly, then delete_option would be the correct way. The question isn’t how to clear the option; the question is how to structure your code such that the “option does not exist” case is a valid case.
Think about it. The first time you start this code, that option isn’t going to exist at all, right? Your code should be perfectly capable of handling that case, since it’s the first thing the user is ever going to see.
get_option() accepts a default value if the option does not exist. So use that. If you had an empty array for the default, for example, you’d have code like this:
Assuming you’re using the settings API, then you should use the
isset
function in if statements to account for the missing-field case. Something like this:And handle each case of actual use of the option accordingly.
Do you want to unset just one option, or do you want to unset every option in the array?
If it’s the former:
If it’s the latter:
first off, understand that every time you do a call to
update_option()
anddelete_option()
– not sure aboutdelete_option()
though – the validation function is called. And the waytccl_settings_domains_validate()
is validating is what’s causing theupdate_option()
function to add more options:what this function is doing is to take whatever is inside *tccl_settings_domains* and assign it to
$options
:$options = get_option('tccl_settings_domains');
then add/push whatever is in
$input
into$options
array. When the function returns$options
(which basically “saves” the option) what’s saved are the contents of options field plus the value of$input
. Thus, the array gets bigger.So, try this for a start:
then, in the Add Domain form (one that goes to options.php), add this hidden input
<input name="tccl_settings_domains[action]" type="hidden" value="add-domain" />
This is the most straightforward solution I could think of that involves minimum amount of change in your code.
let me know if that works for you =)