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();
}
}
我附上了设置屏幕的图片,供您想象我正在从事什么样的项目。