1

I have created an admin theme settings page where I save all my settings in wp_options. Now I want to expand this settings page by plugins which can be activated and deactivated depending on which settings this theme needs to have. For example different payment providers per country.

My settings page option fields are ordered by tabs as for example general, payment, measurement etc.

So I have to make 3 things hookable:

  1. Tabs
  2. The content of a tab
  3. The saving of the tab content

The first 2 (Tabs and tab content) be successful, but the saving of the tab content not. I think this is because I hook into the load-(page) action hook. I have tested all kinds of things, but cannot find why this content does not get saved.

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:

In the bottom of this part you see that I inserted the do_action() function for extra tab content saving.

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

The save add_action hook in my plugin file

function ac_theme_settings_tab_save( $tab, $settings, $postdata ) {

    if( $tab == 'adcalls' ) {
        $settings['gtp_adcalls_domain_id'] = $postdata['gtp_adcalls_domain_id'];
    }

}
add_action( 'gtp_theme_settings_tab_save', 'ac_theme_settings_tab_save', 10, 3 );
0

I have changed the do_action() for a apply_filter(). This worked as follows:

// Filter save settings
$settings = apply_filters( 'gtp_theme_settings_tab_save', $settings, $tab, $_POST );
$updated = update_option( 'gtp_theme_settings', $settings );  

And then my add_filter in plugin file looks like:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.