sofyansitorus / wp-yes
WordPress Yet Easy Settings 是一个PHP类,用于轻松构建WordPress的高级管理页面。
Requires
- php: >=5.6.3
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-05 22:59:55 UTC
README
WordPress Yet Easy Settings 是一个PHP类,用于轻松构建WordPress的高级管理页面。
内置设置字段类型
- 文本
- URL
- 电子邮件
- 密码
- 数字
- 小数
- 多行文本框
- 复选框
- 多选框
- 下拉菜单
- 多选下拉菜单
- 单选按钮
- 颜色选择器
- 文件上传
- 所见即所得
主要功能
- 添加尽可能多的管理页面,可以将其放置在任何位置,无论是顶级管理菜单还是子菜单。
- 添加自定义回调以渲染自定义设置字段类型。
- 添加自定义回调以渲染自定义标签内容。
- 添加自定义回调以渲染自定义页面内容。
- 内置数据清洗和验证。
- 轻松为管理页面添加帮助标签。
- 轻松为管理页面添加自定义操作按钮。
- 排入自定义脚本和样式。
- 内置数据验证。
如何使用
安装
composer require sofyansitorus/wp-yes
在包含WP_Yes类文件之后,您只需要初始化WP_Yes类,然后按照顺序添加设置对象属性、添加标签、添加部分和字段。
简单的管理页面设置
这是初始化设置页面而不定义标签和部分的简单方法。
if ( ! function_exists( 'wp_yes_simple' ) ) { function wp_yes_simple() { $settings = new WP_Yes( 'wp_yes_simple' ); // Initialize the WP_Yes class. $settings->add_field( array( 'id' => 'field_1', 'label' => 'Field 1', 'required' => true, 'type' => 'text', ) ); $settings->add_field( array( 'id' => 'field_2', 'label' => 'Field 2', 'required' => false, 'type' => 'email', ) ); $settings->init(); // Run the WP_Yes class. } } add_action( 'init', 'wp_yes_simple' );
我们还可以通过使用WP_Yes::add_fields
方法批量添加设置字段。
if ( ! function_exists( 'wp_yes_simple_bulk' ) ) { function wp_yes_simple_bulk() { $settings = new WP_Yes( 'wp_yes_simple_bulk' ); // Initialize the WP_Yes class. $settings->add_fields( array( array( 'id' => 'field_1', 'label' => 'Field 1', 'required' => true, 'type' => 'text', ), array( 'id' => 'field_2', 'label' => 'Field 2', 'required' => true, 'type' => 'email', ) ) ); $settings->init(); // Run the WP_Yes class. } } add_action( 'init', 'wp_yes_simple_bulk' );
带部分的简单管理页面设置
这是初始化设置页面而不定义标签和部分的简单方法。
if ( ! function_exists( 'wp_yes_simple_with_section' ) ) { function wp_yes_simple_with_section() { $settings = new WP_Yes( 'wp_yes_simple_with_section' ); // Initialize the WP_Yes class. $settings->add_section( array( 'id' => 'section_1', 'title' => 'Section 1', ) ); $settings->add_fields( array( array( 'id' => 'field_1', 'label' => 'Field 1', 'required' => true, 'type' => 'text', ), array( 'id' => 'field_2', 'label' => 'Field 2', 'required' => true, 'type' => 'email', ) ) ); $settings->add_section( array( 'id' => 'section_2', 'title' => 'Section 2', ) ); $settings->add_fields( array( array( 'id' => 'field_3', 'label' => 'Field 3', 'required' => true, 'type' => 'text', ), array( 'id' => 'field_4', 'label' => 'Field 4', 'required' => true, 'type' => 'email', ) ) ); $settings->init(); // Run the WP_Yes class. } } add_action( 'init', 'wp_yes_simple_with_section' );
多标签管理页面设置
默认情况下,设置页面只有一个标签。如果您想添加更多标签,只需在每个标签的最后一个WP_Yes::add_field
之后调用WP_Yes::add_tab
方法,然后依次调用WP_Yes::add_section
和WP_Yes::add_field
方法。
if ( ! function_exists( 'wp_yes_multi_tabs' ) ) { function wp_yes_multi_tabs() { $settings = new WP_Yes( 'wp_yes_multi_tabs' ); // Initialize the WP_Yes class. $settings->add_tab( array( 'id' => 'tab_1', 'title' => 'Tab 1', ) ); $settings->add_section( array( 'id' => 'section_1', 'title' => 'Section 1', ) ); $settings->add_field( array( 'id' => 'field_1', 'required' => true, 'type' => 'text', ) ); $settings->add_section( array( 'id' => 'section_2', 'title' => 'Section 2', ) ); $settings->add_field( array( 'id' => 'field_2', 'label' => 'Field 2', 'required' => true, 'type' => 'text', ) ); $settings->add_tab( // <-- Add tab 2. array( 'id' => 'tab_2', 'title' => 'Tab 2', ) ); $settings->add_fields( array( array( 'id' => 'field_3', 'label' => 'Field 3', 'required' => true, 'type' => 'text', ), array( 'id' => 'field_4', 'label' => 'Field 4', 'required' => true, 'type' => 'email', ) ) ); $settings->init(); // Run the WP_Yes class. } } add_action( 'init', 'wp_yes_multi_tabs' );
请注意,您需要在WP_Yes构造函数中传递的menu_slug
参数和WP_Yes::add_field
方法参数中的字段id
键具有唯一值。您可以在不同的页面菜单中具有相同的标签id,也可以在不同的标签中具有相同的部分id。
带自定义操作按钮和帮助标签的管理页面设置
要向管理页面添加帮助标签和自定义操作按钮,您需要在调用WP_Yes::init
方法之前,在任何地方调用WP_Yes::add_help_tab
和WP_Yes::add_button
方法。
if ( ! function_exists( 'wp_yes_button_and_help_tabs' ) ) { function wp_yes_button_and_help_tabs() { $settings = new WP_Yes( 'wp_yes_button_and_help_tabs' ); // Initialize the WP_Yes class. $settings->add_help_tab( // <-- Add help tab 1. array( 'id' => 'my_help_tab', 'title' => __( 'My Help Tab' ), 'content' => '<p>' . __('Descriptive content that will show in My Help Tab-body goes here.') . '</p>', ) ); $settings->add_help_tab( // <-- Add help tab 2. array( 'id' => 'my_help_tab2', 'title' => __( 'My Help Tab2' ), 'content' => '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here 2. ') . '</p>', ) ); $settings->add_field( array( 'id' => 'field_1', 'label' => 'Field 1', ) ); $settings->add_button( 'Custom Action Button', 'index.php' ); // <-- Add custom action button. $settings->init(); // Run the WP_Yes class. } } add_action( 'init', 'wp_yes_button_and_help_tabs' );
获取存储的选项值
通过调用内置WordPress get_option
函数并使用字段id作为第一个参数来获取选项值。
get_option( 'field_1' );
如果您在WP_Yes构造函数中将$setting_prefix值设置为第三个参数,或者使用WP_Yes::set_prefix
方法设置前缀,那么在调用get_option函数时需要预先添加该前缀。
if ( ! function_exists( 'wp_yes_with_prefix' ) ) { function wp_yes_with_prefix() { $settings = new WP_Yes( 'wp_yes_with_prefix', array(), 'my_setting_prefix' ); // Initialize the WP_Yes class. $settings->add_field( array( 'id' => 'field_1', 'label' => 'Field 1', ) ); $settings->init(); // Run the WP_Yes class. } } add_action( 'init', 'wp_yes_with_prefix' ); // To get stored option value for setting field field_1 get_option( 'my_setting_prefix_field_1' );
if ( ! function_exists( 'wp_yes_with_set_prefix' ) ) { function wp_yes_with_set_prefix() { $settings = new WP_Yes( 'wp_yes_with_set_prefix', array() ); // Initialize the WP_Yes class. $settings->set_prefix( 'my_setting_prefix2' ); $settings->add_field( array( 'id' => 'field_1', 'label' => 'Field 1', ) ); $settings->init(); // Run the WP_Yes class. } } add_action( 'init', 'wp_yes_with_set_prefix' ); // To get stored option value for setting field field_1 get_option( 'my_setting_prefix2_field_1' );
有关添加自定义标签内容、添加自定义页面内容等更高级示例,请参阅示例文件夹中可用的所有示例。