wordpress通过register_setting新建菜单页面学习笔记
发布:smiling 来源: PHP粉丝网 添加日期:2018-12-02 16:15:27 浏览: 评论:0
在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" />
- //phpfensi.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" />
- //phpfensi.com
- </form>
- </div>
- <?php
- }
- }
- new Create_Test_Menu_Page();
- ?>
Tags: register_setting 新建菜单
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)