erichk4 / settings-framework
设置框架
v1.0.7
2022-12-04 15:24 UTC
Requires
- php: >=7.4.0
README
设置框架旨在通过创建设置 API 的包装来简化为您的插件创建设置页面的痛苦,从而使得创建和维护设置页面变得非常简单。
设置您的插件
- 使用
composer require erichk4/settings-framework
安装设置框架 - 在您的插件根目录中创建一个 "settings" 文件夹。
- 在新的 "settings" 文件夹中创建一个设置文件(例如
settings-general.php
)
现在您可以这样设置您的插件
class SFTest { /** * @var string */ private $plugin_path; /** * @var WordPressSettingsFramework */ private $settings; /** * WPSFTest constructor. */ function __construct() { $this->plugin_path = plugin_dir_path( __FILE__ ); // Create a new SettingsFramework $this->settings = new SettingsFramework( $this->plugin_path . 'settings/settings-general.php', // the settings file 'prefix_settings_general', // the option group $this ); // the caller object // Add admin menu add_action( 'admin_menu', array( $this, 'add_settings_page' ), 20 ); // Add an optional settings validation filter (recommended) add_filter( $this->settings->get_option_group() . '_settings_validate', array( &$this, 'validate_settings' ) ); } /** * Add settings page. */ function add_settings_page() { $this->settings->add_settings_page( array( 'parent_slug' => 'woocommerce', 'page_title' => __( 'Page Title', 'text-domain' ), 'menu_title' => __( 'menu Title', 'text-domain' ), 'capability' => 'manage_woocommerce', ) ); } /** * Validate settings. * * @param $input * * @return mixed */ function validate_settings( $input ) { // Do your settings validation here // Same as $sanitize_callback from http://codex.wordpress.org/Function_Reference/register_setting return $input; } // ... }
您可以通过以下方式访问设置值
// Get settings $this->settings->get_settings();
这将获取保存的设置值,或您在设置文件中设置的默认值。
或通过获取单个设置
// Get individual setting $setting = wpsf_get_setting( 'prefix_settings_general', 'general', 'text' );
设置文件
设置文件通过以下格式填充全局 $wpsf_settings
数组来工作
add_filter( 'wpsf_register_settings_prefix_settings_general', 'prefix_settings_general_settings' ); function prefix_settings_general_settings( $settings ) { // Tabs. $settings[ 'tabs' ] = array( array( 'id' => 'tab1', 'title' => esc_html__( 'Tab1', 'domain' ), ), array( 'id' => 'tab2', 'title' => esc_html__( 'Tab2', 'domain' ), ), ); $settings[] = array( 'tab_id' => 'tab1', 'section_id' => 'general', // The section ID (required) 'section_title' => 'General Settings', // The section title (required) 'section_description' => 'Some intro description about this section.', // The section description (optional) 'section_order' => 5, // The order of the section (required) 'fields' => array( array( 'id' => 'text', 'title' => 'Text', 'desc' => 'This is a description.', 'placeholder' => 'This is a placeholder.', 'type' => 'text', 'default' => 'This is the default value' ), array( 'id' => 'select', 'title' => 'Select', 'desc' => 'This is a description.', 'type' => 'select', 'default' => 'green', 'choices' => array( 'red' => 'Red', 'green' => 'Green', 'blue' => 'Blue' ) ), // add as many fields as you need... ) ); return $settings; }
有效的 fields
值有
id
- 字段 IDtitle
- 字段标题desc
- 字段描述placeholder
- 字段占位符type
- 字段类型(文本/密码/文本区域/选择/单选按钮/复选框/颜色/文件/编辑器/代码编辑器)default
- 默认值(或选中的选项)choices
- 选项数组(用于选择/单选按钮/复选框)mimetype
- Code Mirror 可接受的任何有效 MIME 类型(用于代码编辑器)
有关可能的值的示例,请参阅 settings/example-settings.php
。
API 详细信息
new WordPressSettingsFramework( string $settings_file [, string $option_group = ''] )
根据设置文件创建一个新的设置 option_group。
$settings_file
- 设置文件路径$option_group
- 可选的 "option_group" 覆盖(默认情况下,这将被设置为设置文件的基名)
wpsf_get_setting( $option_group, $section_id, $field_id )
从一个选项组获取设置
$option_group
- 选项组 ID。$section_id
- 部分 ID(使用选项卡时,更改为[{$tab_id}_{$section_id}]
)$field_id
- 字段 ID。
wpsf_delete_settings( $option_group )
从选项组中删除所有保存的设置
$option_group
- 选项组 ID
操作 & 过滤器
过滤器
wpsf_register_settings_[option_group]
- 用于注册设置的自定义过滤器。有关示例,请参阅settings/example-settings.php
。[option_group]_settings_validate
- 基本上是 register_setting 的$sanitize_callback
。使用$wpsf->get_option_group()
获取选项组 ID。wpsf_defaults_[option_group]
- 设置字段的默认参数
操作
wpsf_before_field_[option_group]
- 在字段 HTML 输出之前wpsf_before_field_[option_group]_[field_id]
- 在字段 HTML 输出之前wpsf_after_field_[option_group]
- 在字段 HTML 输出之后wpsf_after_field_[option_group]_[field_id]
- 在字段 HTML 输出之后wpsf_before_settings_[option_group]
- 在设置表单 HTML 输出之前wpsf_after_settings_[option_group]
- 在设置表单 HTML 输出之后wpsf_before_settings_fields_[option_group]
- 在设置表单字段 HTML 输出之前(在<form>
内部)wpsf_do_settings_sections_[option_group]
- 设置表单字段 HTML 输出(在<form>
内部)wpsf_do_settings_sections_[option_group]
- 设置表单字段 HTML 输出(在<form>
内部)wpsf_before_tab_links_[option_group]
- 在选项卡 HTML 输出之前wpsf_after_tab_links_[option_group]
- 在选项卡 HTML 输出之后
致谢
WordPress 设置框架由来自 Dev7studios 的 Gilbert Pellegrom 创建,并由来自 Iconic 的 James Kemp 维护。
想要表示感谢吗?请考虑 给我打赏。