将UPDATE_OPTION()与REGISTER_SETTING()结合使用时的更新问题

时间:2016-06-03 作者:Robbert

Intro

我有一个设置页面,其中包括一个带有更复杂回调函数的设置。

Save data

要保存数据,我使用register_setting() 具有自定义清理回调的函数。我完全按照WordPress设置API的解释实现了它,所以这很好。

Remove data from option

为了从这个选项数组中删除数据,我将jQuery与AJAX回调函数结合使用。

The problem

问题是我不能使用update_option() (在我的AJAX回调中)当register_setting() 也处于活动状态。我知道这一点,因为当我把register_setting() 功能update_option() 突然工作正常,在未注释register\\u setting()的情况下删除该选项。

是否有人有此问题的经验,并且知道如何使用这两个函数来更新选项?

Setup and register setting

function gtp_init_theme_options() { 

    $page = \'services-settings-page\';

    /**
     * Services settings sections and fields
     */
    $section = \'installing_settings_section\';

    // Add installation settings section
    add_settings_section( $section, _x( \'Installing\', \'Measure and installing\', \'gtp_translate\' ), \'gtp_display_installing_settings_section\', $page );

    // Add and register installation areas
    $id = \'installing_data\';
    add_settings_field( $id, __( \'Installing data\', \'gtp_translate\' ), \'gtp_settings_installing_data_fields\', $page, $section, array( \'id\' => $id, \'label_for\' => $id ) );
    register_setting( \'services-theme-settings\', $id, \'gtp_register_installing_data_setting\' ); 

}
add_action( \'admin_init\', \'gtp_init_theme_options\' );

The sanitize callback called by the register_setting()

/**
 * Sanatizes callback for saving installation areas
 */
function gtp_register_installing_data_setting() {

    // Initialize object
    $installing     = new Installing();

    $error = false;

    // Check if country isset
    if ( ! empty( $_POST[\'existing_country\'] ) ) {
        $country = strtolower( $_POST[\'existing_country\'] );
    } elseif ( ! empty( $_POST[\'country\'] ) ) {
        $country = strtolower( $_POST[\'country\'] );
    } else {
        $error = true;  
    }

    // Check if zipcode isset
    if ( ! empty( $_POST[\'existing_zipcode_area\'] ) ) {
        $range = $_POST[\'existing_zipcode_area\'];
    } elseif ( ! empty( $_POST[\'zipcode_from\'] ) && ! empty( $_POST[\'zipcode_to\'] ) ) {
        $range = $_POST[\'zipcode_from\'] . \'-\' . $_POST[\'zipcode_to\'];
    } else {
        $error = true;  
    }

    // Check if product isset
    if ( ! empty( $_POST[\'existing_product\'] ) ) {
        $product = strtolower( $_POST[\'existing_product\'] );    
    } elseif ( ! empty( $_POST[\'product\'] ) ) {
        $product = strtolower( $_POST[\'product\'] );
    } else {
        $error = true;  
    }

    // Check if price isset
    if ( ! empty( $_POST[\'price\'] ) ) {
        $price = str_replace( \',\', \'.\', $_POST[\'price\'] );
    } else {
        $error = true;  
    }

    // No errors
    if ( ! $error ) {

        // Add row to data array
        $installing->addRow( $country, $range, $product, $price );

        // Return data array
        return $installing->getData();

    }

}
/**
 * Sanatizes callback for saving installation areas
 */
function gtp_register_installing_data_setting() {

    // Initialize object
    $installing     = new Installing();

    $error = false;

    // Check if country isset
    if ( ! empty( $_POST[\'existing_country\'] ) ) {
        $country = strtolower( $_POST[\'existing_country\'] );
    } elseif ( ! empty( $_POST[\'country\'] ) ) {
        $country = strtolower( $_POST[\'country\'] );
    } else {
        $error = true;  
    }

    // Check if zipcode isset
    if ( ! empty( $_POST[\'existing_zipcode_area\'] ) ) {
        $range = $_POST[\'existing_zipcode_area\'];
    } elseif ( ! empty( $_POST[\'zipcode_from\'] ) && ! empty( $_POST[\'zipcode_to\'] ) ) {
        $range = $_POST[\'zipcode_from\'] . \'-\' . $_POST[\'zipcode_to\'];
    } else {
        $error = true;  
    }

    // Check if product isset
    if ( ! empty( $_POST[\'existing_product\'] ) ) {
        $product = strtolower( $_POST[\'existing_product\'] );    
    } elseif ( ! empty( $_POST[\'product\'] ) ) {
        $product = strtolower( $_POST[\'product\'] );
    } else {
        $error = true;  
    }

    // Check if price isset
    if ( ! empty( $_POST[\'price\'] ) ) {
        $price = str_replace( \',\', \'.\', $_POST[\'price\'] );
    } else {
        $error = true;  
    }

    // No errors
    if ( ! $error ) {

        // Add row to data array
        $installing->addRow( $country, $range, $product, $price );

        // Return data array
        return $installing->getData();

    }

}
 我附上了设置屏幕的图片,供您想象我正在从事什么样的项目。

enter image description here

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

注意在中提供清理回调时会发生什么register_setting(). 它注册一个过滤器来清理您的选项

add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
现在当你这样做的时候update_option() 然后触发您自己的函数以防止保存:D

因为update_option() 呼叫$value = sanitize_option( $option, $value );

Solution:去除register_settings() 呼叫前回拨update_option().

function gtp_remove_installing_data() {

    //Your code 

    //Remove sanitizing for adding
    remove_filter( "sanitize_option_installing_data", \'gtp_register_installing_data_setting\' );

    // Update 
    $installing->update();

    die;        
}

SO网友:cjbj

这只是一种预感,但在我看来,这个问题与调用操作的顺序有关。

如果定义了挂钩wp_ajax_remove_installing_datawp_ajax_nopriv_remove_installing_data 在您的插件中,并使用init 钩子,这意味着此代码在register_setting, 你钩住的admin_init, 这是later in the queue.

现在,您直接更新选项,而不是通过Options API。正如您在codex page on update_option, 这意味着缓存未更新。因此,您正在删除该选项,但当register_setting 进一步调用,它从缓存中获取值并再次设置。

解决办法是clear the cache 更新选项后,或挂起register_setting 在里面init 并确保其优先级高于更新函数,以便在调用register_setting. 免责声明:我没有尝试过这个。

相关推荐

Huge wp_options table

我有一个WP网站的问题。由于没有更多可用磁盘空间,网站崩溃。搜索时,我检测到wp\\U选项表大小为12GB,但大约只有1100行:有什么想法吗?提前感谢[UPDATE 1]如果我导出wp\\U选项表,拖放并导入,大小将减少到9,7mb:我没有机会用优化表OPTIMIZE TABLE wp_options 但如果再发生的话我会试试的[UPDATE 2]问题仍然存在。我试着OPTIMIZE TABLE wp_options;无结果: