sofyansitorus/wp-yes
Composer 安装命令:
composer require sofyansitorus/wp-yes
包简介
WordPress Yet Easy Settings class is PHP class for easy to build advanced admin page for WordPress.
README 文档
README
WordPress Yet Easy Settings class is PHP class for easy to build advanced admin page for WordPress.
Built-in setting field types
- Text
- URL
- Password
- Number
- Decimal
- Textarea
- Checkbox
- Multiple Checkbox
- Select
- Multiple Select
- Radio Button
- Color Picker
- File Upload
- WYSIWYG
Key features
- Add as many as admin pages, placed it any where as top level admin menu or sub-menu.
- Add custom callback to render custom setting field type.
- Add custom callback to render custom tab content.
- Add custom callback to render custom page content.
- Built-in data sanitation and validation.
- Easy to add help tabs for admin page.
- Easy to add custom action button for admin page.
- Enqueue custom scripts and styles.
- Built-in data validation.
How to Use
Installation:
composer require sofyansitorus/wp-yes
After you include the WP_Yes class file, all you have to do is to initialize the WP_Yes class, then add the settings object properties in sequence add tabs, add sections, add fields.
Simple admin page setting
This is the simplest way to initialize the setting page without defining the tabs and sections.
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' );
We can also add the setting fields in bulk by using the WP_Yes::add_fields method.
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' );
Simple admin page setting with Section
This is the simplest way to initialize the setting page without defining the tabs and sections.
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' );
Multiple tabs admin page setting
By default, the setting page will only has 1 tab. If you want to add more tabs, just simply call the WP_Yes::add_tab method after the last WP_Yes::add_field for each tabs, and then following in sequence calling WP_Yes::add_section and WP_Yes::add_field method.
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' );
A note you must keep in hand here is that you need to have a unique value for the menu_slug parameter that passed in the WP_Yes class constructor and field id key in the WP_Yes::add_field method parameter. You can have same tab id in different page menu, also can has same sections id in different tabs.
Admin page setting with custom action button and help tabs
To add help tabs and custom actin button to the admin page, you need to call WP_Yes::add_help_tab and WP_Yes::add_button method anywhere before calling the WP_Yes::init method.
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' );
Getting the stored option value
To get the option value is by call built-in WordPress get_option function with filed id as the first argument.
get_option( 'field_1' );
If you set the $setting_prefix value at third arguments in WP_Yes constructor, or set the prefix with WP_Yes::set_prefix method, then you need to pre-pend that prefix when calling get_option* function.
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' );
For more advanced example such as adding custom tab content, adding custom page content, etc, please take a look all the samples available in example folder.
Screenshots
Simple Setting Form
All Fields Types
Setting Form with Tabs
Admin Page with Action Button
Admin Page with Help Tabs
Custom Tab Content
Custom Page Content
Sub-menu Admin Page
sofyansitorus/wp-yes 适用场景与选型建议
sofyansitorus/wp-yes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sofyansitorus/wp-yes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sofyansitorus/wp-yes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 62
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2019-12-09