Tuesday, August 9, 2022
HomeWordPress Developmentadmin - Settings API - Final two tabs not rendered separate

admin – Settings API – Final two tabs not rendered separate


I am new to the WP settings API, and a reasonably novice PHP developer. I wished to create a plugin the place I present inputs grouped below a number of tabs. I managed to create 3 tabs however when 4th tab and its fields had been added, they did not present up in a fourth tab however had been rendered throughout the third tab. I wrote the code time and again, so my guess is that the error lies in one of many two capabilities on the finish of the code beneath. Any assistance is appreciated.

class Settings_API_Tabs {

non-public $general_settings_key = 'general_settings';
non-public $hardware_settings_key = 'hardware_settings';
non-public $rollout_key = 'rollout_settings';
non-public $software_key = 'software_settings';

non-public $plugin_options_key = 'device-options';
non-public $plugin_settings_tabs = array();

operate __construct() {
    add_action( 'init', array( &$this, 'load_settings' ) );

    add_action( 'admin_init', array( &$this, 'register_general_settings' ) );
    add_action( 'admin_init', array( &$this, 'register_hardware_settings' ) );
    add_action( 'admin_init', array( &$this, 'register_software_settings' ) );
    add_action( 'admin_init', array( &$this, 'register_rollout_settings' ) );

    add_action( 'admin_menu', array( &$this, 'add_admin_menus' ) );
}

operate load_settings() {
    $this->general_settings     = (array) get_option( $this->general_settings_key );
    $this->hardware_settings    = (array) get_option( $this->hardware_settings_key );
    $this->software_settings    = (array) get_option( $this->software_settings_key );
    $this->rollout_settings     = (array) get_option( $this->rollout_settings_key );
    
    $this->general_settings = array_merge( array(
        'general_option' => 'Normal worth'
    ), $this->general_settings );

    $this->hardware_settings = array_merge( array(
        'hardware_option' => '{Hardware} worth'
    ), $this->hardware_settings );

    $this->software_settings = array_merge( array(
        'software_option' => 'Software program worth'
    ), $this->software_settings );

    $this->rollout_settings = array_merge( array(
        'rollout_option' => 'Rollout worth'
    ), $this->rollout_settings );
}

operate register_general_settings() {
    $this->plugin_settings_tabs[$this->general_settings_key] = 'Normal';

    register_setting( $this->general_settings_key, $this->general_settings_key );
    add_settings_section( 'section_general', 'Normal Plugin Settings', array( &$this, 'section_general_desc' ), $this->general_settings_key );
    add_settings_field( 'general_option', 'A Normal Possibility', array( &$this, 'field_general_option' ), $this->general_settings_key, 'section_general' );
}

operate register_hardware_settings() {
    $this->plugin_settings_tabs[$this->hardware_settings_key] = '{Hardware}';

    register_setting( $this->hardware_settings_key, $this->hardware_settings_key );
    add_settings_section( 'section_hardware', '{Hardware} Plugin Settings', array( &$this, 'section_hardware_desc' ), $this->hardware_settings_key );
    add_settings_field( 'hardware_option', 'A {Hardware} Possibility', array( &$this, 'field_hardware_option' ), $this->hardware_settings_key, 'section_hardware' );
}

operate register_software_settings() {
    $this->plugin_settings_tabs[$this->software_settings_key] = 'Software program';

    register_setting( $this->software_settings_key, $this->software_settings_key );
    add_settings_section( 'section_software', 'Software program Plugin Settings', array( &$this, 'section_software_desc' ), $this->software_settings_key );
    add_settings_field( 'software_option', 'An Software program Possibility', array( &$this, 'field_software_option' ), $this->software_settings_key, 'section_software' );
}

operate register_rollout_settings() {
    $this->plugin_settings_tabs[$this->rollout_settings_key] = 'Rollout';

    register_setting( $this->rollout_settings_key, $this->rollout_settings_key );
    add_settings_section( 'section_rollout', 'Rollout Plugin Settings', array( &$this, 'section_rollout_desc' ), $this->rollout_settings_key );
    add_settings_field( 'rollout_option', 'A Rollout Possibility', array( &$this, 'field_rollout_option' ), $this->rollout_settings_key, 'section_rollout' );
}

operate section_general_desc()     { echo 'Normal part description goes right here.'; }
operate section_hardware_desc()    { echo '{Hardware} part description goes right here.'; }
operate section_software_desc()    { echo 'Software program part description goes right here.'; }
operate section_rollout_desc()     { echo 'Rollout part description goes right here.'; }

operate field_general_option() {
    ?>
    <enter sort="textual content" title="<?php echo $this->general_settings_key; ?>[general_option]" worth="<?php echo esc_attr( $this->general_settings['general_option'] ); ?>" />
    <?php
}

operate field_hardware_option() {
    ?>
    <enter sort="textual content" title="<?php echo $this->hardware_settings_key; ?>[hardware_option]" worth="<?php echo esc_attr( $this->hardware_settings['hardware_option'] ); ?>" />
    <?php
}

operate field_software_option() {
    ?>
    <enter sort="textual content" title="<?php echo $this->software_settings_key; ?>[software_option]" worth="<?php echo esc_attr( $this->software_settings['software_option'] ); ?>" />
    <?php
}

operate field_rollout_option() {
    ?>
    <enter sort="textual content" title="<?php echo $this->rollout_settings_key; ?>[rollout_option]" worth="<?php echo esc_attr( $this->rollout_settings['rollout_option'] ); ?>" />
    <?php
}

operate add_admin_menus() {
    add_options_page( 'DEVICE Settings', 'DEVICE Settings', 'manage_options', $this->plugin_options_key, array( &$this, 'plugin_options_page' ) );
}

operate plugin_options_page() {

    $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->general_settings_key;
    ?>
    <div class="wrap">
        <?php $this->plugin_options_tabs(); ?>
        <type methodology="publish" motion="choices.php">
            <?php wp_nonce_field( 'update-options' ); ?>
            <?php settings_fields( $tab ); ?>
            <?php do_settings_sections( $tab ); ?>
            <?php submit_button(); ?>
        </type>
    </div>
    <?php
}

operate plugin_options_tabs() {
    $current_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->general_settings_key;

    screen_icon();
    echo '<h2 class="nav-tab-wrapper">';
    foreach ( $this->plugin_settings_tabs as $tab_key => $tab_caption ) {
        $energetic = $current_tab == $tab_key ? 'nav-tab-active' : '';
        echo '<a category="nav-tab ' . $energetic . '" href="?web page=" . $this->plugin_options_key . "&tab=' . $tab_key . '">' . $tab_caption . '</a>';
    }
    echo '</h2>';
}
};

add_action( 'plugins_loaded', create_function( '', '$settings_api_tabs_demo_plugin = new Settings_API_Tabs;' ) );

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments