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

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

下面是其展示图界面:

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 :

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()
{
?>

请在该面板填写你的信息

option_item); $text_string = $options['text']; ?>

My Plugin

option_group); //Prints out all settings sections added to a particular settings page do_settings_sections('lgc_mypluginss'); ?>


6. 添加钩子:


add_action('admin_menu',array($this,'my_lgc_test_plugin_page'));

add_action('admin_init',array($this,'register_setting_page'));



7. 完整代码逻辑:


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()
{
?>

请在该面板填写你的信息

option_item); $text_string = $options['text']; ?>

My Plugin

option_group); //Prints out all settings sections added to a particular settings page do_settings_sections('lgc_mypluginss'); ?>




你可能感兴趣的:(wordpress)