首页 > CMS > Wordpress > 正文

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

2024-09-07 00:52:40
字体:大 中 小
来源:转载
供稿:网友

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表