麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 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. <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. //Vevb.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. //Vevb.com 
  63. </form> 
  64. </div> 
  65. <?php 
  66.  
  67. new Create_Test_Menu_Page(); 
  68. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久国产精品视频 | 最新亚洲国产 | 日韩在线播放第一页 | 激情影院在线观看 | 久久精品99国产国产精 | 欧美一级黄 | 国产免费观看一区二区三区 | 国产免费一区二区三区 | 在线亚洲播放 | 深夜激情视频 | 无码av女优 | 黄色的视频免费观看 | 在线观看美女av | 亚洲爱爱图| 免费a级毛片永久免费 | 国产91一区 | 国产精品av久久久久久无 | 亚洲人成网站免费播放 | 91懂色| 成人区一区二区 | 精品国产一区二区三区久久久狼牙 | 黄色片在线免费播放 | 2019中文字幕在线播放 | 久久久线视频 | 永久免费黄色大片 | 高清国产在线 | 成人性生活视频在线观看 | 一级毛片免费在线 | 精品国产一区二区三区天美传媒 | 午夜偷拍视频 | 成年人高清视频在线观看 | 国产精品视频一区二区三区综合 | 91午夜视频 | 日本xxxx色视频在线观看免费, | 国产在线一级视频 | 91色一区二区三区 | 精品国产一二区 | 超碰97最新 | 九九视屏 | 欧美日韩一区,二区,三区,久久精品 | 视频一区二区三区在线 |