当前位置:首页 > CMS教程 > WordPress > 列表

wordpress通过register_setting新建菜单页面学习笔记

发布:smiling 来源: PHP粉丝网  添加日期:2018-12-02 16:15:27 浏览: 评论:0 

在wordpress开发中经常需要新建菜单页面页面来对数据库进行操作,而register_setting就是其中的一种方式。

而实现的方式 :

1. 新建一个子菜单在“设置”菜单下:

  1. add_options_page('My Plugin','My Plugins','manage_options','lgc_mypluginss',array($this,'my_test_plugins_page_func')); 

2. 注册一个新的setting :

  1. register_setting($this->option_group,$this->option_item); 

格式:register_setting( string $option_group, string $option_name, array $args = array() )

3. 定义一个区域 Defining Sections and Settings : 

  1. 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:

  1. //section 
  2. function lgc_myplugin_section_content_func() 
  3. ?> 
  4. <p>请在该面板填写你的信息</p> 
  5. <?php 
  6.  
  7. //字段 
  8. function lgc_myplugin_test_filed_text() 
  9. $options = get_option($this->option_item); 
  10. $text_string = $options['text']; 
  11. ?> 
  12. <input type="text" name="<?php echo $this->option_item; ?>[text]" id="text" value='<?php echo $text_string; ?>'/> 
  13. <?php 
  14.  
  15. //菜单页面内容 
  16. function my_test_plugins_page_func() 
  17. ?> 
  18. <div class="wrap"
  19. <?php screen_icon(); ?> 
  20. <h2>My Plugin</h2> 
  21. <form action="options.php" method="post"
  22. <?php 
  23. //Ouput setting_field 
  24. settings_fields($this->option_group); 
  25.  
  26. //Prints out all settings sections added to a particular settings page 
  27. do_settings_sections('lgc_mypluginss'); 
  28. ?> 
  29.  
  30. <!--submit--> 
  31. <input type="submit" name="Submit" value="Save Change" class="button button-primary" /> 
  32. //phpfensi.com 
  33. </form> 
  34. </div> 
  35. <?php 

6. 添加钩子:

  1. add_action('admin_menu',array($this,'my_lgc_test_plugin_page')); 
  2.  
  3. add_action('admin_init',array($this,'register_setting_page')); 

7.完整代码逻辑:

  1. <?php 
  2. class Create_Test_Menu_Page{ 
  3.  
  4. var $option_group = 'lgc_my_plugin_options'
  5. var $option_item = 'lgc_my_plugin_options'
  6.  
  7. function __construct(){ 
  8. //创建菜单 
  9. add_action('admin_menu',array($this,'my_lgc_test_plugin_page')); 
  10.  
  11. add_action('admin_init',array($this,'register_setting_page')); 
  12.  
  13. function my_lgc_test_plugin_page() 
  14. //Add submenu page to the Settings main menu 
  15. //add_options_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '' ) 
  16. add_options_page('My Plugin','My Plugins','manage_options','lgc_mypluginss',array($this,'my_test_plugins_page_func')); 
  17.  
  18.  
  19. function register_setting_page() 
  20. ////Registering New Settings 
  21. register_setting($this->option_group,$this->option_item); 
  22.  
  23. //定义一个区域 Defining Sections and Settings 
  24. //add_settings_section( string $id, string $title, callable $callback, string $page ) 
  25. add_settings_section('lgc_myplugin_section','My_Plugin_Settings',array($this,'lgc_myplugin_section_content_func'),'lgc_mypluginss'); 
  26. //add a new field to a section of a settings page 
  27. //add_settings_field( string $id, string $title, callable $callback, string $page, string $section = 'default', array $args = array() ) 
  28. add_settings_field('lgc_myplugin_filed_text','请输入文字',array($this,'lgc_myplugin_test_filed_text'),'lgc_mypluginss','lgc_myplugin_section'); 
  29.  
  30.  
  31. function lgc_myplugin_section_content_func() 
  32. ?> 
  33. <p>请在该面板填写你的信息</p> 
  34. <?php 
  35.  
  36.  
  37. function lgc_myplugin_test_filed_text() 
  38. $options = get_option($this->option_item); 
  39. $text_string = $options['text']; 
  40. ?> 
  41. <input type="text" name="<?php echo $this->option_item; ?>[text]" id="text" value='<?php echo $text_string; ?>'/> 
  42. <?php 
  43.  
  44.  
  45.  
  46. function my_test_plugins_page_func() 
  47. ?> 
  48. <div class="wrap"
  49. <?php screen_icon(); ?> 
  50. <h2>My Plugin</h2> 
  51. <form action="options.php" method="post"
  52. <?php 
  53. //Ouput setting_field 
  54. settings_fields($this->option_group); 
  55.  
  56. //Prints out all settings sections added to a particular settings page 
  57. do_settings_sections('lgc_mypluginss'); 
  58. ?> 
  59.  
  60. <!--submit--> 
  61. <input type="submit" name="Submit" value="Save Change" class="button button-primary" /> 
  62. //phpfensi.com 
  63. </form> 
  64. </div> 
  65. <?php 
  66.  
  67. new Create_Test_Menu_Page(); 
  68. ?> 

Tags: register_setting 新建菜单

分享到: