多个设置 Wordpress 选项页面插件 [英] Multiple settings Wordpress Options Page plugin

查看:25
本文介绍了多个设置 Wordpress 选项页面插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<div class="wrap"><?php screen_icon();?><h2>设置</h2><form method="post" action="options.php"><?php//这会打印出所有隐藏的设置字段settings_fields('test_option_group');do_settings_sections('test-setting-admin');?><?php submit_button();?></表单>

<?php}公共函数 page_init(){register_setting('test_option_group', 'array_key', array($this, 'check_ID'));add_settings_section('setting_section_id','环境',数组($this,'print_section_info'),测试设置管理员");add_settings_field('some_id','一些 ID(标题)',数组($this, 'create_an_id_field'),'测试设置管理员','setting_section_id');}公共函数 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);}别的{update_option('test_some_id', $mid);}}别的{$mid = '';}返回 $mid;}公共函数print_section_info(){打印 '在下面输入您的设置:';}公共函数 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 = 新 wctest();

一切都如页面上的代码所示,但我想添加第二个设置.如何添加另一个设置部分和设置字段并能够保护这些值?我已经困惑了半天了,但没有运气.

有人可以帮我吗?这是我的第一个插件,如果我理解了这部分,我可以做剩下的.

解决方案

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

options = get_option('my_option_name');?><div class="wrap"><?php screen_icon();?><h2>我的设置</h2><form method="post" action="options.php"><?php//这会打印出所有隐藏的设置字段settings_fields('my_option_group');do_settings_sections('我的设置管理员');提交按钮();?></表单>

<?php}/*** 注册并添加设置*/公共函数 page_init(){寄存器设置('my_option_group',//选项组'my_option_name',//选项名称array( $this, 'sanitize' )//消毒);add_settings_section('setting_section_id',//ID'我的自定义设置',//标题array( $this, 'print_section_info' ),//回调'my-setting-admin'//页面);add_settings_field('id_number',//ID'身份证号码',//标题array( $this, 'id_number_callback' ),//回调'my-setting-admin',//页面'setting_section_id'//部分);add_settings_field('标题','标题',数组( $this, 'title_callback' ),'我的设置管理员','setting_section_id');}/*** 根据需要清理每个设置字段** @param array $input 包含所有设置字段作为数组键*/公共函数消毒( $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'] );返回 $new_input;}/*** 打印部分文本*/公共函数print_section_info(){打印 '在下面输入您的设置:';}/*** 获取设置选项数组并打印其值之一*/公共函数 id_number_callback(){打印输出('<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']) : '');}/*** 获取设置选项数组并打印其值之一*/公共函数 title_callback(){打印输出('<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');拉取信息.

I want to make an Options page with multiple settings for my plugin. I want to use this code as a start: 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();

Everything works as shown below the code on the page, but I want to add a second setting. How do I add another settings section and a settings field and being able to safe the values? I have been puzzling for half a day now, but no luck.

Could someone help me out please? This is my first plugin and if I understand this part I can do the rest.

解决方案

I just modified the example in the Codex: Creating_Options_Pages#Example_.232. Now it includes a second Settings Field. The sanitization function is simpler to understand and expand. Also changed the names of the variables and added documentation in the code. I think it's easier to follow the logic now. Here it is in full:

<?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();

If you want to add new Settings Sections, just do it and point the desired Settings Fields to the new section.

Pull the information wherever needed using: get_option( 'my_option_name' );.

这篇关于多个设置 Wordpress 选项页面插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆