多个设置Wordpress选项页面插件

时间:2013-07-22 22:15:13

标签: wordpress wordpress-plugin

我想为我的插件制作一个包含多个设置的“选项”页面。我想将此代码用作开头:http://codex.wordpress.org/Creating_Options_Pages#Example_.232

<?php
class wctest{
    public function __construct(){
        if(is_admin()){
            add_action('admin_menu', array($this, 'add_plugin_page'));
            add_action('admin_init', array($this, 'page_init'));
        }
    }

    public function add_plugin_page(){
        // This page will be under "Settings"
        add_options_page('Settings Admin', 'Settings', 'manage_options', 'test-setting-admin', array($this, 'create_admin_page'));
    }

    public function create_admin_page(){
    ?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2>Settings</h2>           
        <form method="post" action="options.php">
            <?php
                    // This prints out all hidden setting fields
            settings_fields('test_option_group');   
            do_settings_sections('test-setting-admin');
        ?>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
    }

    public function page_init(){        
        register_setting('test_option_group', 'array_key', array($this, 'check_ID'));

        add_settings_section(
            'setting_section_id',
            'Setting',
            array($this, 'print_section_info'),
            'test-setting-admin'
        );  

        add_settings_field(
            'some_id', 
            'Some ID(Title)', 
            array($this, 'create_an_id_field'), 
            'test-setting-admin',
            'setting_section_id'            
        );      
    }

    public function check_ID($input){
        if(is_numeric($input['some_id'])){
            $mid = $input['some_id'];           
            if(get_option('test_some_id') === FALSE){
                add_option('test_some_id', $mid);
            }else{
                update_option('test_some_id', $mid);
            }
        }else{
            $mid = '';
        }
        return $mid;
    }

    public function print_section_info(){
        print 'Enter your setting below:';
    }

    public function create_an_id_field(){
        ?><input type="text" id="input_whatever_unique_id_I_want" name="array_key[some_id]"    value="<?=get_option('test_some_id');?>" /><?php
    }
}

$wctest = new wctest();

一切正常如页面上的代码所示,但我想添加第二个设置。如何添加其他设置部分和设置字段并能够保护值?我现在已经困惑了半天,但没有运气。

有人可以帮帮我吗?这是我的第一个插件,如果我理解这一部分,我可以完成剩下的工作。

2 个答案:

答案 0 :(得分:3)

我刚修改了法典中的例子:Creating_Options_Pages#Example_.232。现在它包含第二个设置字段。清理功能更易于理解和扩展。还更改了代码中变量的名称和添加的文档。我认为现在更容易遵循逻辑。这是完整的:

<?php
class MySettingsPage
{
    /**
     * Holds the values to be used in the fields callbacks 
     */
    private $options;

    /**
     * Start up
     */
    public function __construct()
    {
        add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'page_init' ) );
    }

    /**
     * Add options page
     */
    public function add_plugin_page()
    {
        // This page will be under "Settings"
        add_options_page(
            'Settings Admin', 
            'My Settings', 
            'manage_options', 
            'my-setting-admin', 
            array( $this, 'create_admin_page' )
        );
    }

    /**
     * Options page callback
     */
    public function create_admin_page()
    {
        // Set class property
        $this->options = get_option( 'my_option_name' );
    ?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2>My Settings</h2>           
        <form method="post" action="options.php">
        <?php
            // This prints out all hidden setting fields
            settings_fields( 'my_option_group' );   
            do_settings_sections( 'my-setting-admin' );
            submit_button(); 
        ?>
        </form>
    </div>
    <?php
    }

    /**
     * Register and add settings
     */
    public function page_init()
    {        
        register_setting(
            'my_option_group', // Option group
            'my_option_name', // Option name
            array( $this, 'sanitize' ) // Sanitize
        );

        add_settings_section(
            'setting_section_id', // ID
            'My Custom Settings', // Title
            array( $this, 'print_section_info' ), // Callback
            'my-setting-admin' // Page
        );  

        add_settings_field(
            'id_number', // ID
            'ID Number', // Title 
            array( $this, 'id_number_callback' ), // Callback
            'my-setting-admin', // Page
            'setting_section_id' // Section           
        );      

        add_settings_field(
            'title', 
            'Title', 
            array( $this, 'title_callback' ), 
            'my-setting-admin', 
            'setting_section_id'
        );      
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
        $new_input = array();
        if( isset( $input['id_number'] ) )
            $new_input['id_number'] = absint( $input['id_number'] );

        if( isset( $input['title'] ) )
            $new_input['title'] = sanitize_text_field( $input['title'] );

        return $new_input;
    }

    /** 
     * Print the Section text
     */
    public function print_section_info()
    {
        print 'Enter your settings below:';
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function id_number_callback()
    {
        printf(
            '<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
            isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''
        );
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function title_callback()
    {
        printf(
            '<input type="text" id="title" name="my_option_name[title]" value="%s" />',
            isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
        );
    }
}

if( is_admin() )
    $my_settings_page = new MySettingsPage();

如果要添加新的设置部分,只需执行此操作并将所需的设置字段指向新部分。

使用以下方式提取信息: get_option( 'my_option_name' );

答案 1 :(得分:0)

要添加其他部分,您必须将ID参数(第一个)设置为与第一个设置的ID不同的值。

    add_settings_section(
        'setting_section_new', // ID
        'My Custom Settings', // Title
        array( $this, 'print_section_info' ), // Callback
        'my-setting-admin' // Page
    );