如何设置选项页面的默认值

时间:2016-09-27 作者:An Gagova

这是我的选项页:

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 个回复
SO网友:Ahmed Fouad

这可以通过存储插件的版本并检查该版本是否存在来实现,这意味着插件已安装,如果不存在,则可以设置默认选项“仅一次”。

检查插件是否已安装,如果未安装,则设置默认选项并存储插件版本,以避免在每次加载页面时重置选项

// 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
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

}

SO网友:Benoti

最好是在激活挂钩上设置默认值,如果用户在选项页面上删除此值,则添加回退。

register_activation_hook(   __FILE__, array($this, \'_plugin_activation\') );

public function _plugin_activation(){
    if(!get_option(\'price_list_options_option_name\')) {
    //not present, so add new options
    $options = array(
        \'price_list_section_title_0\'=> \'default title\',
        \'price_list_section_subtitle_1\'=> \'default subtitle\',
        etc...

    );

    add_option(\'price_list_options_option_name\', $options);
  }
}
您可以对“清理”自定义函数和\\u plugin\\u激活选项数组使用相同的选项数组(和默认值),以轻松获得相同的默认值。i、 e:在您的\\uu构造()中,添加,$this->array\\u of\\u default\\u value=array(\'price\\u list\\u section\\u title\\u 0\'=>\'default title\',\'price\\u list\\u section\\u subtitle\\u 1\'=>\'default subtitle\',等等);

从选项页保存选项值时,请选中$\\u POST name。

if(isset($_POST[\'price_list_section_title_0\')== \'\'){
    $default_value = $this->array_of_default_value[\'price_list_section_title_0\'];
    update_option(\'price_list_options_option_name\', \'price_list_section_title_0\', $default_value);
}

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register