default value options page plugin development

Question

I am trying to create a admin settings page for a plugin using add_options_page. The options page works just fine, but I am having issues setting a default value.

I’ve tried putting the default value as value="" in input field:

    public function title_0_callback() {
        printf(
            '<input class="regular-text" type="text" name="ie_blocker_option_name[title_0]" id="title_0" value="The site you are trying to connect to does not support Internet Explorer.">',
            isset( $this->ie_blocker_options['title_0'] ) ? esc_attr( $this->ie_blocker_options['title_0']) : ''
        );
    }

This kinda works, there is a default value but the user has to press "save settings" before the value will take effect.

I have also tried with register_activation_hook but this doesn’t seem to do anything. Not quite sure what I am missing:

// Activation
function name_plugin_activation(){
    do_action( 'name_plugin_default_options' );
}
register_activation_hook( __FILE__, 'name_plugin_activation' );


// Set default values here
function name_plugin_default_values(){

    // Form settings
    add_option('title_0', 'The site you are trying to connect to does not support Internet Explorer.');
    add_option('content_1', 'To continue, please use a modern web browser such as hehe:');


}
add_action( 'name_plugin_default_options', 'name_plugin_default_values' );

Below my full code for admin settings page:

class IEBlocker {
    private $ie_blocker_options;

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

    public function ie_blocker_add_plugin_page() {
        add_options_page(
            'IE Blocker', // page_title
            'IE Blocker', // menu_title
            'manage_options', // capability
            'ie-blocker', // menu_slug
            array( $this, 'ie_blocker_create_admin_page' ) // function
        );
    }

    public function ie_blocker_create_admin_page() {
        $this->ie_blocker_options = get_option( 'ie_blocker_option_name' ); ?>

        <div class="wrap">
            <h2>IE Blocker</h2>
            <p>Just a little intro text.</p>
            <?php settings_errors(); ?>

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

    public function ie_blocker_page_init() {
        register_setting(
            'ie_blocker_option_group', // option_group
            'ie_blocker_option_name', // option_name
            array( $this, 'ie_blocker_sanitize' ) // sanitize_callback
        );

        add_settings_section(
            'ie_blocker_setting_section', // id
            'Settings', // title
            array( $this, 'ie_blocker_section_info' ), // callback
            'ie-blocker-admin' // page
        );

        add_settings_field(
            'title_0', // id
            'Title', // title
            array( $this, 'title_0_callback' ), // callback
            'ie-blocker-admin', // page
            'ie_blocker_setting_section' // section
        );

        add_settings_field(
            'content_1', // id
            'Content', // title
            array( $this, 'content_1_callback' ), // callback
            'ie-blocker-admin', // page
            'ie_blocker_setting_section' // section
        );
    }

    public function ie_blocker_sanitize($input) {
        $sanitary_values = array();
        if ( isset( $input['title_0'] ) ) {
            $sanitary_values['title_0'] = sanitize_text_field( $input['title_0'] );
        }

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

        return $sanitary_values;
    }

    public function ie_blocker_section_info() {
        
    }

    public function title_0_callback() {
        printf(
            '<input class="regular-text" type="text" name="ie_blocker_option_name[title_0]" id="title_0" value="The site you are trying to connect to does not support Internet Explorer.">',
            isset( $this->ie_blocker_options['title_0'] ) ? esc_attr( $this->ie_blocker_options['title_0']) : ''
        );
    }

    public function content_1_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="ie_blocker_option_name[content_1]" id="content_1">To continue, please use a modern web browser such as:</textarea>',
            isset( $this->ie_blocker_options['content_1'] ) ? esc_attr( $this->ie_blocker_options['content_1']) : ''
        );
    }

}
if ( is_admin() )
    $ie_blocker = new IEBlocker();
0
TurboTobias 2 years 2020-10-24T07:10:28-05:00 0 Answers 13 views 0

Leave an answer

Browse
Browse