设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 教程 > 正文

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

发布时间:2022-06-22 10:01 所属栏目:61 来源:互联网
导读:在wordpress开发中经常需要新建菜单页面页面来对数据库进行操作,而register_setting就是其中的一种方式。 而实现的方式 : 1. 新建一个子菜单在设置菜单下: add_options_page(My Plugin,My Plugins,manage_options,lgc_mypluginss,array($this,my_test_plu
  在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();
  ?> 。

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读