清空使用设置API创建的选项的最佳方法是什么?

时间:2011-07-26 作者:jnthnclrk

我正在尝试清空使用设置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;
}
?>

3 个回复
最合适的回答,由SO网友:Otto 整理而成

如果您正确编写了代码,那么delete\\u选项将是正确的方法。问题不在于如何清除选项;问题是如何构造代码,使“选项不存在”案例是有效案例。

想想看。第一次启动此代码时,该选项根本不存在,对吗?您的代码应该完全能够处理这种情况,因为这是用户将看到的第一件事。

如果选项不存在,get\\u option()接受默认值。所以使用它。例如,如果默认值为空数组,则代码如下:

$options = get_option(\'whatever\',array());
假设您使用的是设置API,那么您应该使用isset 函数在if语句中解释缺少的字段大小写。类似这样:

if (!isset($options[\'name\'])) {
//... the option isn\'t set to something 
} else {
//... the option is set to something
}
并相应地处理选项的实际使用情况。

SO网友:Chip Bennett

您是只想取消设置一个选项,还是要取消设置阵列中的每个选项?

如果是前者:

$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 );

SO网友:Ryann

首先,要明白每次你打电话给update_option()delete_option() - 不确定delete_option() 虽然-将调用验证函数。还有路tccl_settings_domains_validate() 正在验证导致update_option() 用于添加更多选项的函数:

 function tccl_settings_domains_validate( $input ) {
   $options = get_option( \'tccl_settings_domains\' );
   $options[] = $input;
   return $options;
 }
此函数的作用是获取*tccl\\u settings\\u domains*中的任何内容并将其分配给$options:

$options = get_option(\'tccl_settings_domains\');

然后添加/推送任何内容$input 进入$options 大堆函数返回时$options (基本上是“保存”选项)保存的是选项字段的内容plus 的价值$input. 因此,阵列变得更大。

因此,首先尝试一下:

function tccl_settings_domains_validate( $input ) {
    if( isset( $input[\'action\'] ) && $input[\'action\'] == \'add-domain\' ){
        //add new value to $options array
        options = get_option( \'tccl_settings_domains\' );
        $options[] = $input;
        return $options;
    }
    //return whatever value is passed.
    return $input;
}
然后,在Add-Domain表单(进入options.php的表单)中,添加这个隐藏的输入<input name="tccl_settings_domains[action]" type="hidden" value="add-domain" />

这是我能想到的最简单的解决方案,它涉及到代码中最小的更改量。

让我知道这是否适合你=)

结束

相关推荐

Search options/filters

我正在尝试向侧栏搜索框添加一些复选框选项,similar to this, 用户可以选择是否搜索All Words, Some Word, 或者Entire phrase.我确实在搜索后找到了这个-Wordpress Search Phrases. “句子”选项似乎很有效,但其他选项则不太好。下面的代码是我目前正在处理的,但如果能得到一些帮助使其正常工作,我将不胜感激。非常感谢S(我不想为此使用插件)。<form action=\"<?php bloginfo(\'home\'); ?>