如何设置默认值插件激活

问题描述

我正在尝试使用add_options_page为插件创建管理员设置页面。选项页面工作正常,但在设置插件激活的认值时遇到问题。

我尝试在输入字段中将认值设置为value =“”:

    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']) : ''
        );
    }

这有点用,有一个认值,但是用户必须按“保存设置”才能使该值生效。

我也尝试过register_activation_hook,它应该检查字段是否为空,然后检查是否为空。这似乎无能为力。不太确定我缺少什么:

// 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(){

    $ie_blocker_options = get_option( 'ie_blocker_option_name' ); // Array of All Options
    $title_0 = $ie_blocker_options['title_0']; // Title
    $content_1 = $ie_blocker_options['content_1']; // Content
    
    $new_value = 'The site you are trying to connect to does not support Internet Explorer.';
     
    if ( get_option( $title_0 ) !== false ) {
     
        // The option already exists,so update it.
        update_option( $title_0,$new_value );
     
    } 

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

在我用于创建选项页/管理员设置页的完整代码下面:

class IEBlocker {
    private $ie_blocker_options;

    public function __construct() {
        add_action( 'admin_menu',array( $this,'ie_blocker_add_plugin_page' ) );
        add_action( 'admin_init','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_0_callback' ),// callback
            'ie-blocker-admin',// page
            'ie_blocker_setting_section' // section
        );

        add_settings_field(
            'content_1',// id
            'Content','content_1_callback' ),// 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();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)