在wordpress开发中经常需要新建菜单页面页面来对数据库进行操作,而register_setting就是其中的一种方式。
而实现的方式 :
1. 新建一个子菜单在“设置”菜单下:
- add_options_page('My Plugin','My Plugins','manage_options','lgc_mypluginss',array($this,'my_test_plugins_page_func'));
 
2. 注册一个新的setting :
- register_setting($this->option_group,$this->option_item);
 
格式:register_setting( string $option_group, string $option_name, array $args = array() )
3. 定义一个区域 Defining Sections and Settings :
- add_settings_section('lgc_myplugin_section','My_Plugin_Settings',array($this,'lgc_myplugin_section_content_func'),'lgc_mypluginss');
 
格式://add_settings_section( string $id, string $title, callable $callback, string $page )
4. add a new field to a section of a settings page
add_settings_field('lgc_myplugin_filed_text','请输入文字',array($this,'lgc_myplugin_test_filed_text'),'lgc_mypluginss','lgc_myplugin_section');
格式://add_settings_field( string $id, string $title, callable $callback, string $page, string $section = 'default', array $args = array() )
5.添加其对应的回调方法callback:
- //section
 - function lgc_myplugin_section_content_func()
 - {
 - ?>
 - <p>请在该面板填写你的信息</p>
 - <?php
 - }
 - //字段
 - function lgc_myplugin_test_filed_text()
 - {
 - $options = get_option($this->option_item);
 - $text_string = $options['text'];
 - ?>
 - <input type="text" name="<?php echo $this->option_item; ?>[text]" id="text" value='<?php echo $text_string; ?>'/>
 - <?php
 - }
 - //菜单页面内容
 - function my_test_plugins_page_func()
 - {
 - ?>
 - <div class="wrap">
 - <?php screen_icon(); ?>
 - <h2>My Plugin</h2>
 - <form action="options.php" method="post">
 - <?php
 - //Ouput setting_field
 - settings_fields($this->option_group);
 - //Prints out all settings sections added to a particular settings page
 - do_settings_sections('lgc_mypluginss');
 - ?>
 - <!--submit-->
 - <input type="submit" name="Submit" value="Save Change" class="button button-primary" />
 - //Vevb.com
 - </form>
 - </div>
 - <?php
 - }
 
6. 添加钩子:
- add_action('admin_menu',array($this,'my_lgc_test_plugin_page'));
 - add_action('admin_init',array($this,'register_setting_page'));
 
7.完整代码逻辑:
- <?php
 - class Create_Test_Menu_Page{
 - var $option_group = 'lgc_my_plugin_options';
 - var $option_item = 'lgc_my_plugin_options';
 - function __construct(){
 - //创建菜单
 - add_action('admin_menu',array($this,'my_lgc_test_plugin_page'));
 - add_action('admin_init',array($this,'register_setting_page'));
 - }
 - function my_lgc_test_plugin_page()
 - {
 - //Add submenu page to the Settings main menu
 - //add_options_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '' )
 - add_options_page('My Plugin','My Plugins','manage_options','lgc_mypluginss',array($this,'my_test_plugins_page_func'));
 - }
 - function register_setting_page()
 - {
 - ////Registering New Settings
 - register_setting($this->option_group,$this->option_item);
 - //定义一个区域 Defining Sections and Settings
 - //add_settings_section( string $id, string $title, callable $callback, string $page )
 - add_settings_section('lgc_myplugin_section','My_Plugin_Settings',array($this,'lgc_myplugin_section_content_func'),'lgc_mypluginss');
 - //add a new field to a section of a settings page
 - //add_settings_field( string $id, string $title, callable $callback, string $page, string $section = 'default', array $args = array() )
 - add_settings_field('lgc_myplugin_filed_text','请输入文字',array($this,'lgc_myplugin_test_filed_text'),'lgc_mypluginss','lgc_myplugin_section');
 - }
 - function lgc_myplugin_section_content_func()
 - {
 - ?>
 - <p>请在该面板填写你的信息</p>
 - <?php
 - }
 - function lgc_myplugin_test_filed_text()
 - {
 - $options = get_option($this->option_item);
 - $text_string = $options['text'];
 - ?>
 - <input type="text" name="<?php echo $this->option_item; ?>[text]" id="text" value='<?php echo $text_string; ?>'/>
 - <?php
 - }
 - function my_test_plugins_page_func()
 - {
 - ?>
 - <div class="wrap">
 - <?php screen_icon(); ?>
 - <h2>My Plugin</h2>
 - <form action="options.php" method="post">
 - <?php
 - //Ouput setting_field
 - settings_fields($this->option_group);
 - //Prints out all settings sections added to a particular settings page
 - do_settings_sections('lgc_mypluginss');
 - ?>
 - <!--submit-->
 - <input type="submit" name="Submit" value="Save Change" class="button button-primary" />
 - //Vevb.com
 - </form>
 - </div>
 - <?php
 - }
 - }
 - new Create_Test_Menu_Page();
 - ?>
 
新闻热点
疑难解答
图片精选