here is my option page:

class PriceListOptions {
private $price_list_options_options;

public function __construct() {
    add_action( 'admin_menu', array( $this, 'price_list_options_add_plugin_page' ) );
    add_action( 'admin_init', array( $this, 'price_list_options_page_init' ) );
}

public function price_list_options_add_plugin_page() {
    add_theme_page(
        'Price list options', // page_title
        'Price list options', // menu_title
        'manage_options', // capability
        'price-list-options', // menu_slug
        array( $this, 'price_list_options_create_admin_page' ) // function
    );
}

public function price_list_options_create_admin_page() {
    $this->price_list_options_options = get_option( 'price_list_options_option_name' ); ?>

    <div class="wrap">
        <h2>Price list options</h2>
        <p>set price list options</p>
        <?php settings_errors(); ?>

        <form method="post" action="options.php">
            <?php
                settings_fields( 'price_list_options_option_group' );
                do_settings_sections( 'price-list-options-admin' );
                submit_button();
            ?>
        </form>
    </div>
<?php }

public function price_list_options_page_init() {
    register_setting(
        'price_list_options_option_group', // option_group
        'price_list_options_option_name', // option_name
        array( $this, 'price_list_options_sanitize' ) // sanitize_callback
    );

    add_settings_section(
        'price_list_options_setting_section', // id
        'Settings', // title
        array( $this, 'price_list_options_section_info' ), // callback
        'price-list-options-admin' // page
    );

    add_settings_field(
        'price_list_section_title_0', // id
        'Price list section title', // title
        array( $this, 'price_list_section_title_0_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_list_section_subtitle_1', // id
        'Price list section subtitle', // title
        array( $this, 'price_list_section_subtitle_1_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_list_section_subtitle_2', // id
        'Price list section subtitle', // title
        array( $this, 'price_list_section_subtitle_2_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_plan_1_title_3', // id
        'Price plan 1 title', // title
        array( $this, 'price_plan_1_title_3_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_currency_4', // id
        'Price currency', // title
        array( $this, 'price_currency_4_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_plan_1_price_5', // id
        'Price plan 1 price', // title
        array( $this, 'price_plan_1_price_5_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_plan_1_period_6', // id
        'Price plan 1 period', // title
        array( $this, 'price_plan_1_period_6_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_plan_1_info_7', // id
        'Price plan 1 info', // title
        array( $this, 'price_plan_1_info_7_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_plan_1_features_8', // id
        'Price plan 1 features', // title
        array( $this, 'price_plan_1_features_8_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_plan_1_button_title_9', // id
        'Price plan 1 button title', // title
        array( $this, 'price_plan_1_button_title_9_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_plan_1_button_url_10', // id
        'Price plan 1 button URL', // title
        array( $this, 'price_plan_1_button_url_10_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );

    add_settings_field(
        'price_plan_1_panel_color_11', // id
        'Price plan 1 panel color', // title
        array( $this, 'price_plan_1_panel_color_11_callback' ), // callback
        'price-list-options-admin', // page
        'price_list_options_setting_section' // section
    );
}

public function price_list_options_sanitize($input) {
    $sanitary_values = array();
    $defaults = array (
'price_list_section_title_0' => 'test',
'do_extra_thing' => false
    );

    if ( isset( $input['price_list_section_title_0'] ) ) {
        $sanitary_values['price_list_section_title_0'] = wp_parse_args(sanitize_text_field( $input['price_list_section_title_0'], $defaults ));
    }



    if ( isset( $input['price_list_section_subtitle_1'] ) ) {
        $sanitary_values['price_list_section_subtitle_1'] = sanitize_text_field( $input['price_list_section_subtitle_1'] );
    }

    if ( isset( $input['price_list_section_subtitle_2'] ) ) {
        $sanitary_values['price_list_section_subtitle_2'] = sanitize_text_field( $input['price_list_section_subtitle_2'] );
    }

    if ( isset( $input['price_plan_1_title_3'] ) ) {
        $sanitary_values['price_plan_1_title_3'] = sanitize_text_field( $input['price_plan_1_title_3'] );
    }

    if ( isset( $input['price_currency_4'] ) ) {
        $sanitary_values['price_currency_4'] = sanitize_text_field( $input['price_currency_4'] );
    }

    if ( isset( $input['price_plan_1_price_5'] ) ) {
        $sanitary_values['price_plan_1_price_5'] = sanitize_text_field( $input['price_plan_1_price_5'] );
    }

    if ( isset( $input['price_plan_1_period_6'] ) ) {
        $sanitary_values['price_plan_1_period_6'] = sanitize_text_field( $input['price_plan_1_period_6'] );
    }

    if ( isset( $input['price_plan_1_info_7'] ) ) {
        $sanitary_values['price_plan_1_info_7'] = sanitize_text_field( $input['price_plan_1_info_7'] );
    }

    if ( isset( $input['price_plan_1_features_8'] ) ) {
        $sanitary_values['price_plan_1_features_8'] = esc_textarea( $input['price_plan_1_features_8'] );
    }

    if ( isset( $input['price_plan_1_button_title_9'] ) ) {
        $sanitary_values['price_plan_1_button_title_9'] = sanitize_text_field( $input['price_plan_1_button_title_9'] );
    }

    if ( isset( $input['price_plan_1_button_url_10'] ) ) {
        $sanitary_values['price_plan_1_button_url_10'] = sanitize_text_field( $input['price_plan_1_button_url_10'] );
    }

    if ( isset( $input['price_plan_1_panel_color_11'] ) ) {
        $sanitary_values['price_plan_1_panel_color_11'] = sanitize_text_field( $input['price_plan_1_panel_color_11'] );
    }

    return $sanitary_values;
}

public function price_list_options_section_info() {

}

public function price_list_section_title_0_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_list_section_title_0]" id="price_list_section_title_0" value="%s">',
        isset( $this->price_list_options_options['price_list_section_title_0'] ) ? esc_attr( $this->price_list_options_options['price_list_section_title_0']) : ''
    );
}

public function price_list_section_subtitle_1_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_list_section_subtitle_1]" id="price_list_section_subtitle_1" value="%s">',
        isset( $this->price_list_options_options['price_list_section_subtitle_1'] ) ? esc_attr( $this->price_list_options_options['price_list_section_subtitle_1']) : ''
    );
}

public function price_list_section_subtitle_2_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_list_section_subtitle_2]" id="price_list_section_subtitle_2" value="%s">',
        isset( $this->price_list_options_options['price_list_section_subtitle_2'] ) ? esc_attr( $this->price_list_options_options['price_list_section_subtitle_2']) : ''
    );
}

public function price_plan_1_title_3_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_plan_1_title_3]" id="price_plan_1_title_3" value="%s">',
        isset( $this->price_list_options_options['price_plan_1_title_3'] ) ? esc_attr( $this->price_list_options_options['price_plan_1_title_3']) : ''
    );
}

public function price_currency_4_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_currency_4]" id="price_currency_4" value="%s">',
        isset( $this->price_list_options_options['price_currency_4'] ) ? esc_attr( $this->price_list_options_options['price_currency_4']) : ''
    );
}

public function price_plan_1_price_5_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_plan_1_price_5]" id="price_plan_1_price_5" value="%s">',
        isset( $this->price_list_options_options['price_plan_1_price_5'] ) ? esc_attr( $this->price_list_options_options['price_plan_1_price_5']) : ''
    );
}

public function price_plan_1_period_6_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_plan_1_period_6]" id="price_plan_1_period_6" value="%s">',
        isset( $this->price_list_options_options['price_plan_1_period_6'] ) ? esc_attr( $this->price_list_options_options['price_plan_1_period_6']) : ''
    );
}

public function price_plan_1_info_7_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_plan_1_info_7]" id="price_plan_1_info_7" value="%s">',
        isset( $this->price_list_options_options['price_plan_1_info_7'] ) ? esc_attr( $this->price_list_options_options['price_plan_1_info_7']) : ''
    );
}

public function price_plan_1_features_8_callback() {
    printf(
        '<textarea class="large-text" rows="5" name="price_list_options_option_name[price_plan_1_features_8]" id="price_plan_1_features_8">%s</textarea>',
        isset( $this->price_list_options_options['price_plan_1_features_8'] ) ? esc_attr( $this->price_list_options_options['price_plan_1_features_8']) : ''
    );
}

public function price_plan_1_button_title_9_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_plan_1_button_title_9]" id="price_plan_1_button_title_9" value="%s">',
        isset( $this->price_list_options_options['price_plan_1_button_title_9'] ) ? esc_attr( $this->price_list_options_options['price_plan_1_button_title_9']) : ''
    );
}

public function price_plan_1_button_url_10_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_plan_1_button_url_10]" id="price_plan_1_button_url_10" value="%s">',
        isset( $this->price_list_options_options['price_plan_1_button_url_10'] ) ? esc_attr( $this->price_list_options_options['price_plan_1_button_url_10']) : ''
    );
}

public function price_plan_1_panel_color_11_callback() {
    printf(
        '<input class="regular-text" type="text" name="price_list_options_option_name[price_plan_1_panel_color_11]" id="price_plan_1_panel_color_11" value="%s">',
        isset( $this->price_list_options_options['price_plan_1_panel_color_11'] ) ? esc_attr( $this->price_list_options_options['price_plan_1_panel_color_11']) : ''
    );
}

}



// Parse incomming $args into an array and merge it with $defaults

if ( is_admin() )
    $price_list_options = new PriceListOptions();

/* 
 * Retrieve this value with:
 * $price_list_options_options = get_option( 'price_list_options_option_name' ); // Array of All Options
 * $price_list_section_title_0 = $price_list_options_options['price_list_section_title_0']; // Price list section title
 * $price_list_section_subtitle_1 = $price_list_options_options['price_list_section_subtitle_1']; // Price list section subtitle
 * $price_list_section_subtitle_2 = $price_list_options_options['price_list_section_subtitle_2']; // Price list section subtitle
 * $price_plan_1_title_3 = $price_list_options_options['price_plan_1_title_3']; // Price plan 1 title
 * $price_currency_4 = $price_list_options_options['price_currency_4']; // Price currency
 * $price_plan_1_price_5 = $price_list_options_options['price_plan_1_price_5']; // Price plan 1 price
 * $price_plan_1_period_6 = $price_list_options_options['price_plan_1_period_6']; // Price plan 1 period
 * $price_plan_1_info_7 = $price_list_options_options['price_plan_1_info_7']; // Price plan 1 info
 * $price_plan_1_features_8 = $price_list_options_options['price_plan_1_features_8']; // Price plan 1 features
 * $price_plan_1_button_title_9 = $price_list_options_options['price_plan_1_button_title_9']; // Price plan 1 button title
 * $price_plan_1_button_url_10 = $price_list_options_options['price_plan_1_button_url_10']; // Price plan 1 button URL
 * $price_plan_1_panel_color_11 = $price_list_options_options['price_plan_1_panel_color_11']; // Price plan 1 panel color
 */
?>

2 Answers
2

This can be done by storing a version for your plugin and check if that version exists, it means the plugin is installed and if it does not exist then you can setup default options “once only”.

Check if plugin is installed, if it is not then setup default options and store a plugin version to avoid resetting your options on every page load

// check for new installation and setup default options
add_action( 'init', 'check_plugin_version' );
function check_plugin_version() {

    // this will run your default option setup if the version does not exist
    if ( ! defined( 'IFRAME_REQUEST' ) && get_option( 'my_plugin_version' ) == null ) {
        setup_options();
        update_option( 'my_plugin_version', '1.0.0' );
    }
}

Setup default options

// setup default options
function setup_options() {

    // here you can save your default options
    // the first time your plugin will run

    update_option( 'myoption_id1', '...' );
    update_option( 'myoption_id2', '...' );
    //etc

}

Leave a Reply

Your email address will not be published. Required fields are marked *