我已经创建了一个管理主题设置页面,在这里我将所有设置保存在wp\\U选项中。现在我想通过插件扩展这个设置页面,插件可以根据主题需要的设置来激活和停用。例如,每个国家有不同的支付提供商。
“我的设置”页面选项字段按选项卡排序,例如“常规”、“付款”、“度量”等。
因此,我必须让三件事可以挂钩:
制表符的内容保存制表符内容前2个(制表符和制表符内容)成功,但saving 选项卡内容的。我想这是因为我load-(page) 行动挂钩。我已经测试了各种内容,但找不到为什么这些内容没有保存。
Admin init:
function gtp_admin_init() {
$settings = get_option( \'gtp_theme_settings\' );
if( empty( $settings ) ) {
$settings = array();
add_option( \'gtp_theme_settings\', $settings, \'\', \'yes\' );
}
}
add_action( \'init\', \'gtp_admin_init\' );
Create admin settings page and load settings page by gtp_load_settings_page():
function gtp_settings_page_init() {
$settings_page = add_theme_page( __( \'Theme settings\', \'gtp_translate\' ), __( \'Theme settings\', \'gtp_translate\' ), \'edit_theme_options\', \'theme-settings\', \'gtp_settings_page\' );
add_action( \'load-\' . $settings_page, \'gtp_load_settings_page\' );
}
add_action( \'admin_menu\', \'gtp_settings_page_init\' );
Load right tab page after submitting data:
function gtp_load_settings_page() {
if( isset( $_POST[\'submit\'] ) ) {
check_admin_referer( \'gtp-settings-page\' );
gtp_save_theme_settings();
$url_parameters = isset( $_GET[\'tab\'] ) ? \'updated=true&tab=\' . $_GET[\'tab\'] : \'updated=true\';
wp_redirect( admin_url( \'themes.php?page=theme-settings&\' . $url_parameters ) );
exit;
}
}
Save settings + action hook:
在这部分的底部,您可以看到我插入了do\\u action()函数来保存额外的选项卡内容。
function gtp_save_theme_settings() {
global $pagenow;
// Get settings from options table
$settings = get_option( \'gtp_theme_settings\' );
// Check which tab is active
if( $pagenow == \'themes.php\' && $_GET[\'page\'] == \'theme-settings\' ) {
if( isset( $_GET[\'tab\'] ) )
$tab = $_GET[\'tab\'];
else
$tab = \'general\';
}
// Check which data to save
switch( $tab ) {
case \'general\' :
$settings[\'gtp_contact_redirect\'] = $_POST[\'gtp_contact_redirect\'];
$settings[\'gtp_review_redirect\'] = $_POST[\'gtp_review_redirect\'];
break;
case \'payment\' :
$settings[\'gtp_payment_setting\'] = $_POST[\'gtp_payment_setting\'];
break;
}
// Hook save settings
do_action( \'gtp_theme_settings_tab_save\', $tab, $settings, $_POST );
$updated = update_option( \'gtp_theme_settings\', $settings );
}
一
function gtp_load_settings_page() {
if( isset( $_POST[\'submit\'] ) ) {
check_admin_referer( \'gtp-settings-page\' );
gtp_save_theme_settings();
$url_parameters = isset( $_GET[\'tab\'] ) ? \'updated=true&tab=\' . $_GET[\'tab\'] : \'updated=true\';
wp_redirect( admin_url( \'themes.php?page=theme-settings&\' . $url_parameters ) );
exit;
}
}
最合适的回答,由SO网友:Robbert 整理而成
我已更改了apply\\u筛选器()的do\\u操作()。其工作原理如下:
// Filter save settings
$settings = apply_filters( \'gtp_theme_settings_tab_save\', $settings, $tab, $_POST );
$updated = update_option( \'gtp_theme_settings\', $settings );
然后,插件文件中的add\\u过滤器如下所示:
function ac_theme_settings_tab_save( $settings, $tab, $postdata ) {
if( $tab == \'adcalls\' ) {
$settings[\'gtp_adcalls_domain_id\'] = $postdata[\'gtp_adcalls_domain_id\'];
}
return $settings;
}
add_filter( \'gtp_theme_settings_tab_save\', \'ac_theme_settings_tab_save\', 10, 3 );