|
1 | | -# WordPress Setting Fields Library |
| 1 | +# WordPress Register Setting Fields |
2 | 2 |
|
3 | | -A WordPress library for creating settings pages with tabs, sections, branded headers, and 30+ field types. Handles |
4 | | -registration, rendering, sanitization, and the Settings API — you just define your fields. |
| 3 | +Settings pages for the WordPress admin, from a field configuration. |
| 4 | + |
| 5 | +Registration, rendering, sanitization and the Settings API are handled; you describe the fields. Every type comes from |
| 6 | +[wp-field-kit](https://github.com/arraypress/wp-field-kit), so the same configuration renders the same control here, in |
| 7 | +a metabox, on a term screen or in a flyout. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- Every field type the kit has — sixty-odd, from text to a repeater to an email editor |
| 12 | +- Tabs and sections, with the tab remembered in the URL |
| 13 | +- A branded header with a logo, badge and whatever you want beside it |
| 14 | +- Conditional fields, encrypted values, and constants that override an option |
| 15 | +- Reset, export and import, when you want them |
| 16 | +- Values readable through helpers that know what a field meant, not just what it stored |
| 17 | + |
| 18 | +## Requirements |
| 19 | + |
| 20 | +- PHP 7.4+ |
| 21 | +- WordPress 5.8+ |
5 | 22 |
|
6 | 23 | ## Installation |
7 | 24 |
|
8 | 25 | ```bash |
9 | 26 | composer require arraypress/wp-register-setting-fields |
10 | 27 | ``` |
11 | 28 |
|
12 | | -## Quick Start |
| 29 | +## Quick start |
13 | 30 |
|
14 | 31 | ```php |
15 | 32 | register_setting_fields( 'my_plugin', [ |
16 | | - 'page_title' => 'My Plugin Settings', |
17 | | - 'menu_title' => 'My Plugin', |
| 33 | + 'page_title' => __( 'My Plugin Settings', 'my-plugin' ), |
| 34 | + 'menu_title' => __( 'My Plugin', 'my-plugin' ), |
18 | 35 | 'parent_slug' => 'options-general.php', |
19 | 36 | 'fields' => [ |
20 | | - 'site_name' => [ 'type' => 'text', 'label' => 'Site Name' ], |
21 | | - 'enable_feature' => [ 'type' => 'toggle', 'label' => 'Enable Feature' ], |
22 | | - 'items_per_page' => [ 'type' => 'number', 'label' => 'Items Per Page', 'min' => 1, 'max' => 100 ], |
| 37 | + 'site_name' => [ 'type' => 'text', 'label' => __( 'Site name', 'my-plugin' ) ], |
| 38 | + 'enable_feature' => [ 'type' => 'toggle', 'label' => __( 'Enable feature', 'my-plugin' ) ], |
| 39 | + 'items_per_page' => [ |
| 40 | + 'type' => 'number', |
| 41 | + 'label' => __( 'Items per page', 'my-plugin' ), |
| 42 | + 'min' => 1, |
| 43 | + 'max' => 100, |
| 44 | + ], |
23 | 45 | ], |
24 | 46 | ] ); |
| 47 | +``` |
25 | 48 |
|
| 49 | +Reading them back: |
| 50 | + |
| 51 | +```php |
26 | 52 | $name = get_setting_field_value( 'my_plugin', 'site_name' ); |
27 | 53 |
|
28 | 54 | if ( is_setting_on( 'my_plugin', 'enable_feature' ) ) { |
29 | | - // Feature is enabled |
| 55 | + // ... |
30 | 56 | } |
31 | 57 | ``` |
32 | 58 |
|
33 | | -## Documentation |
| 59 | +## Registration |
34 | 60 |
|
35 | | -Full documentation is available at **[https://arraypress.github.io/wp-register-setting-fields](https://arraypress.github.io/wp-register-setting-fields)** |
| 61 | +```php |
| 62 | +register_setting_fields( 'my_plugin', [ |
| 63 | + 'page_title' => __( 'My Plugin Settings', 'my-plugin' ), |
| 64 | + 'menu_title' => __( 'My Plugin', 'my-plugin' ), |
| 65 | + 'menu_slug' => 'my-plugin', |
| 66 | + 'parent_slug' => 'options-general.php', |
| 67 | + 'capability' => 'manage_options', |
| 68 | + 'option_name' => 'my_plugin', |
| 69 | + 'icon' => 'dashicons-admin-generic', |
| 70 | + 'position' => null, |
| 71 | + 'submit_button' => true, |
| 72 | + 'reset_button' => false, |
| 73 | + 'export_import' => false, |
| 74 | + 'tabs' => [ /* ... */ ], |
| 75 | + 'sections' => [ /* ... */ ], |
| 76 | + 'fields' => [ /* ... */ ], |
| 77 | +] ); |
| 78 | +``` |
36 | 79 |
|
37 | | -## Requirements |
| 80 | +| Key | What it does | |
| 81 | +| --- | --- | |
| 82 | +| `page_title` / `menu_title` | The page's heading and its menu entry. | |
| 83 | +| `menu_slug` | The page slug. Defaults to the id. | |
| 84 | +| `parent_slug` | Where it sits. A top-level menu when empty. | |
| 85 | +| `capability` | Who may see it. Defaults to `manage_options`. | |
| 86 | +| `option_name` | The option every value is stored in. Defaults to the id. | |
| 87 | +| `option_group` | The Settings API group. Defaults to `{id}_group`. | |
| 88 | +| `constant_prefix` | Constants that override stored values. Defaults to `{option_name}_`. | |
| 89 | +| `header_title`, `logo`, `badge` | A branded header instead of the plain one. | |
| 90 | +| `icon`, `position` | Menu icon and position, for a top-level page. | |
| 91 | +| `submit_button` | Whether to draw one. | |
| 92 | +| `reset_button` | A "restore defaults" button. | |
| 93 | +| `export_import` | Buttons to download and upload the settings as JSON. | |
| 94 | +| `help_tabs`, `help_sidebar` | Contextual help, in core's own panel. | |
38 | 95 |
|
39 | | -- PHP 7.4+ |
40 | | -- WordPress 5.8+ |
| 96 | +## Tabs and sections |
| 97 | + |
| 98 | +A field says which tab and which section it belongs to. Both are optional and can be mixed. |
| 99 | + |
| 100 | +```php |
| 101 | +register_setting_fields( 'my_plugin', [ |
| 102 | + 'tabs' => [ |
| 103 | + 'general' => __( 'General', 'my-plugin' ), |
| 104 | + 'advanced' => __( 'Advanced', 'my-plugin' ), |
| 105 | + ], |
| 106 | + 'sections' => [ |
| 107 | + 'branding' => [ |
| 108 | + 'title' => __( 'Branding', 'my-plugin' ), |
| 109 | + 'description' => __( 'How it looks.', 'my-plugin' ), |
| 110 | + ], |
| 111 | + ], |
| 112 | + 'fields' => [ |
| 113 | + 'site_name' => [ |
| 114 | + 'type' => 'text', |
| 115 | + 'label' => __( 'Site name', 'my-plugin' ), |
| 116 | + 'tab' => 'general', |
| 117 | + 'section' => 'branding', |
| 118 | + ], |
| 119 | + 'debug' => [ |
| 120 | + 'type' => 'toggle', |
| 121 | + 'label' => __( 'Debug mode', 'my-plugin' ), |
| 122 | + 'tab' => 'advanced', |
| 123 | + ], |
| 124 | + ], |
| 125 | +] ); |
| 126 | +``` |
| 127 | + |
| 128 | +## Conditional fields |
| 129 | + |
| 130 | +```php |
| 131 | +'fields' => [ |
| 132 | + 'mode' => [ |
| 133 | + 'type' => 'select', |
| 134 | + 'label' => __( 'Mode', 'my-plugin' ), |
| 135 | + 'options' => [ 'auto' => __( 'Automatic', 'my-plugin' ), 'manual' => __( 'Manual', 'my-plugin' ) ], |
| 136 | + ], |
| 137 | + 'api_key' => [ |
| 138 | + 'type' => 'password', |
| 139 | + 'label' => __( 'API key', 'my-plugin' ), |
| 140 | + 'depends' => [ 'mode' => 'manual' ], |
| 141 | + ], |
| 142 | +], |
| 143 | +``` |
| 144 | + |
| 145 | +The field is removed from the tab order when hidden, not merely made invisible. |
| 146 | + |
| 147 | +## Encrypted values |
| 148 | + |
| 149 | +```php |
| 150 | +'api_secret' => [ |
| 151 | + 'type' => 'password', |
| 152 | + 'label' => __( 'API secret', 'my-plugin' ), |
| 153 | + 'encrypted' => true, |
| 154 | +], |
| 155 | +``` |
| 156 | + |
| 157 | +Stored encrypted, refused REST exposure, and decrypted on the way back out through `get_setting_field_value()`. |
| 158 | + |
| 159 | +## Constants |
| 160 | + |
| 161 | +Any value can be overridden by a constant, which is how a secret stays out of the database on a production install. |
| 162 | + |
| 163 | +```php |
| 164 | +// In wp-config.php: |
| 165 | +define( 'MY_PLUGIN_API_KEY', 'sk-live-...' ); |
| 166 | +``` |
| 167 | + |
| 168 | +`get_setting_field_value( 'my_plugin', 'api_key' )` returns the constant, and the field renders read-only with a note |
| 169 | +saying where the value came from. |
| 170 | + |
| 171 | +## Reading values |
| 172 | + |
| 173 | +| Function | Returns | |
| 174 | +| --- | --- | |
| 175 | +| `get_setting_field_value( $id, $key, $fallback = null )` | One value, with its default and any constant applied. | |
| 176 | +| `get_all_setting_values( $id )` | Everything, keyed by field. | |
| 177 | +| `is_setting_on( $id, $key )` | Whether a toggle or checkbox is on. | |
| 178 | +| `is_setting_enabled( $id, $key, $option )` | Whether an option is among a multiple field's values. | |
| 179 | +| `get_setting_field_list( $id, $key )` | A list field's values as an array. | |
| 180 | +| `get_setting_field_ids( $id, $key )` | A relational field's ids, as integers. | |
| 181 | +| `get_setting_field_page_url( $id, $key )` | The permalink of a page a field points at. | |
| 182 | +| `is_setting_field_page( $id, $key )` | Whether the current request is that page. | |
| 183 | +| `update_setting_field_value( $id, $key, $value )` | Write one, sanitized by its own type. | |
| 184 | +| `delete_setting_field_value( $id, $key )` | Remove one, so it falls back to its default. | |
| 185 | + |
| 186 | +Email and licence fields have their own: `get_setting_fields_email()`, `is_setting_fields_email_enabled()`, |
| 187 | +`get_setting_field_license_status()`, `is_setting_field_license_active()`. |
| 188 | + |
| 189 | +## Custom sanitization |
| 190 | + |
| 191 | +A field's type sanitizes it. Supply a callback to replace that entirely: |
| 192 | + |
| 193 | +```php |
| 194 | +'slug' => [ |
| 195 | + 'type' => 'text', |
| 196 | + 'label' => __( 'Slug', 'my-plugin' ), |
| 197 | + 'sanitize_callback' => fn( $value ) => sanitize_title( $value ), |
| 198 | +], |
| 199 | +``` |
| 200 | + |
| 201 | +The callback receives the raw value, not one the type has already cleaned. |
41 | 202 |
|
42 | 203 | ## License |
43 | 204 |
|
44 | | -GPL-2.0-or-later |
| 205 | +GPL-2.0-or-later |
0 commit comments