dwnload / wp-settings-api
A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.
Requires
- php: ^8.4
- ext-json: *
- thefrosty/wp-utilities: ^3.11 || ^4.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- phpcompatibility/php-compatibility: *
- phpunit/php-code-coverage: ^11
- phpunit/phpunit: ^11.1
- rector/rector: ^2.0
- roots/wordpress: ^7.1
- slevomat/coding-standard: ~8.18
- squizlabs/php_codesniffer: ^3.2
- wp-coding-standards/wpcs: ^3.1
- wp-phpunit/wp-phpunit: ^7.1
- yoast/phpunit-polyfills: ^4.0
Suggests
- frontpack/composer-assets-plugin: Composer plugin for copying of frontend assets into public directory.
Provides
None
Conflicts
None
Replaces
None
- dev-develop
- v4.2.1
- v4.2.0
- v4.1.0
- v4.0.0
- v4.0-RC2
- v4.0-RC1
- v3.12.1
- v3.12.0
- v3.11.1
- v3.11.0
- v3.10.0
- v3.9.0
- v3.8.2
- v3.8.1
- v3.8.0
- v3.7.0
- v3.6.1
- v3.6.0
- v3.5.0
- v3.4.1
- v3.4.0
- v3.3.2
- v3.3.1
- 3.3.0
- 3.2.3
- 3.2.2
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.5
- 2.4.3
- 2.4.2
- 2.4.0
- 2.2.0
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- dev-feature/network-secret-fields
- dev-feature/secrets-api
This package is auto-updated.
Last update: 2026-11-24 22:34:34 UTC
README
It's a PHP class wrapper for handling WordPress Settings API.
Package Installation (via Composer)
To install this package, edit your composer.json file:
{
"require": {
"dwnload/wp-settings-api": "^4.0"
}
}
Now run:
$ composer install dwnload/wp-settings-api
Usage Example
@see examples/Example.php
Network settings pages (multisite)
Pass PluginSettings::NETWORK => true to register the page under Network Admin → Settings instead of
Settings. All of its sections are stored as network options (get_site_option()), and they never mix with
per-site options. A plugin that needs both registers two instances: one site page and one network page.
$network = new WpSettingsApi(SettingsApiFactory::create([ 'domain' => 'my-plugin', 'file' => __FILE__, 'menu-slug' => 'my-plugin-network', 'menu-title' => 'My Plugin', 'page-title' => 'My Plugin Network Settings', 'prefix' => 'my_plugin_', 'version' => '1.0.0', PluginSettings::NETWORK => true, ])); // Read a network value from any site (including the front end): $license = Options::getNetworkOption('license', 'my_plugin_network');
- Saving goes through
wp-admin/network/edit.php?action=dwnload_wp_settings_api_{menu-slug}with the same nonce, sanitizing (register_setting()callbacks), and "Settings saved." notice asoptions.php. - The default capability is
manage_network_options(dwnload/wp_settings_api/capabilityfilter, which now also receives thePluginSettings). - On a single site, a network page behaves like a regular site page (network options are options there).
Secret fields (WordPress 7.2+ Secrets API)
Use FieldTypes::FIELD_TYPE_SECRET ('secret') for API keys and other credentials:
$field_manager->addField(new SettingField([ SettingField::NAME => 'api_key', SettingField::LABEL => 'API key', SettingField::TYPE => FieldTypes::FIELD_TYPE_SECRET, SettingField::SECTION_ID => $section_id, ])); $api_key = Options::getSecret('api_key', $section_id); // ?string
- WordPress 7.2+ (
wp_set_secret()exists): the value is encrypted with the Secrets API under the name{section_id}/{field_id}, and the option only stores awp-secret:placeholder. The value is never sent back to the browser; a blank submission keeps it, and a "Delete" checkbox removes it. Saving requires themanage_secretscapability and aWP_SECRETS_KEYinwp-config.php. If core returns an error, nothing is saved and the error is shown. A plaintext value saved before the upgrade is moved into the Secrets API on the next save. - Older WordPress: it behaves like an obfuscated password field stored in the option.
- Filters:
dwnload/wp_settings_api/use_secrets_api(bool) anddwnload/wp_settings_api/secret_name(string). - On a network settings page, secret fields always use the obfuscated network option fallback: core's network
secrets use separate functions and the
manage_network_secretscapability, which aren't supported yet.
Suggested package
⭐️ frontpack/composer-assets-plugin
As of v3.2.1, this shouldn't be needed any more (unless you are filtering local assets only). Assets will now be loaded via the jsdelivr CDN.
As of v4.0, CDN assets get a Subresource Integrity
integrity attribute when the installed package version matches WpSettingsApi::VERSION. The hash is taken from the
installed copy of each file, so the browser refuses a CDN file that doesn't match it. In that case the unminified file
is served, because jsDelivr generates the .min files itself. To turn it off:
add_filter('dwnload/wp_settings_api/asset_integrity', '__return_false');
Otherwise, since this is a PHP package and not a WordPress plugin the assets included can't be loaded properly.
In order to have the settings page inherit the styles and use the proper JS, you've got to copy the
/assets directory to your plugin or theme. Then add the following to filter the asset src to your
directory:
<?php declare(strict_types=1); namespace Vendor\Package; use Dwnload\WpSettingsApi\Api\Script; use Dwnload\WpSettingsApi\Api\Style; use Dwnload\WpSettingsApi\WpSettingsApi; use TheFrosty\WpUtilities\Plugin\AbstractHookProvider; use TheFrosty\WpUtilities\Plugin\HooksTrait; /** * Class WpSettingsApi * * @package Dwnload\WpSettingsApi */ class WpSettingsApiScripts extends AbstractHookProvider { public function addHooks(array $scripts): void { \add_filter(WpSettingsApi::FILTER_PREFIX . 'admin_scripts', [$this, 'adminScripts']); \add_filter(WpSettingsApi::FILTER_PREFIX . 'admin_styles', [$this, 'adminStyles']); } /** * The default script needs to be moved from the vendor directory somewhere into our app since the * vendor directory is outside of the doc root. * @param Script[] $scripts * @return array */ public function adminScripts(array $scripts): array { \array_walk($scripts, function (Script $script, int $key) use (&$scripts) { switch ($script->getHandle()) { case WpSettingsApi::ADMIN_SCRIPT_HANDLE: /** * If you're not using the `TheFrosty\WpUtilities\Plugin\AbstractHookProvider` * use `plugins_url()` in place of the `$this->getPlugin()->getUrl` or any other WP * function that will point to the asset. * (Should match `frontpack/composer-assets-plugin configs`) */ $scripts[$key]->setSrc($this->getPlugin()->getUrl('assets/js/admin.js')); break; case WpSettingsApi::ADMIN_MEDIA_HANDLE: /** * If you're not using the `TheFrosty\WpUtilities\Plugin\AbstractHookProvider` * use `plugins_url()` in place of the `$this->getPlugin()->getUrl` or any other WP * function that will point to the asset. * (Should match `frontpack/composer-assets-plugin configs`) */ $scripts[$key]->setSrc($this->getPlugin()->getUrl('assets/js/wp-media-uploader.js')); break; } $this->registerScript($script); }); return $scripts; } /** * The default style needs to be moved from the vendor directory somewhere into our app since the * vendor directory is outside of the doc root. * @param Style[] $styles * @return array */ public function adminStyles(array $styles): array { \array_walk($styles, function (Style $style, int $key) use (&$styles) { if ($style->getHandle() === WpSettingsApi::ADMIN_STYLE_HANDLE) { /** * If you're not using the `TheFrosty\WpUtilities\Plugin\AbstractHookProvider` * use `plugins_url()` in place of the `$this->getPlugin()->getUrl` or any other WP * function that will point to the asset. */ $styles[$key]->setSrc($this->getPlugin()->getUrl('assets/css/admin.css')); $this->registerStyle($style); } }); return $styles; } /** * If the script is not registered before being returned back to the filter the src still uses * the vendor directory file path. * @param Script $script */ private function registerScript(Script $script): void { \wp_register_script( $script->getHandle(), $script->getSrc(), $script->getDependencies(), $script->getVersion(), $script->getInFooter() ); } /** * If the style is not registered before being returned back to the filter the src still uses * the vendor directory file path. * @param Style $style */ private function registerStyle(Style $style): void { \wp_register_style( $style->getHandle(), $style->getSrc(), $style->getDependencies(), $style->getVersion(), $style->getMedia() ); } }