PHPackages                             sergeliatko/wpsettings - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. sergeliatko/wpsettings

ActiveLibrary[Framework](/categories/framework)

sergeliatko/wpsettings
======================

WordPress UI Framework to speed up development of settings pages in admin area.

v0.3.0(1mo ago)10263GPL-3.0-or-laterPHPPHP &gt;=7.3

Since Apr 23Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/sergeliatko/wpsettings)[ Packagist](https://packagist.org/packages/sergeliatko/wpsettings)[ Docs](https://github.com/sergeliatko/html)[ GitHub Sponsors](https://github.com/sponsors/sergeliatko)[ RSS](/packages/sergeliatko-wpsettings/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (16)Used By (0)

WPSettings
==========

[](#wpsettings)

WordPress Settings API Framework
--------------------------------

[](#wordpress-settings-api-framework)

This PHP package helps speed up the development of your settings screens in WordPress admin. The framework allows you to easily add options, settings sections and admin pages to WordPress admin, including managing registering, sanitizing, properly adding and displaying your settings according to both WordPress Settings API and best coding standards.

Who this is useful for
----------------------

[](#who-this-is-useful-for)

WPSettings framework is intended to help plugin and theme developers who need to quickly create consistent admin interfaces in WordPress no matter how large or small their projects. It is especially useful for developers who use WordPress as a "spare wheel" and may not be very experienced with WordPress specifics and its proprietary logic.

Is it overkill to use a framework such as this that helps you interact with WordPress Settings API? Is it secure to rely on code that you do not yourself control?

Following more than 10 years of deep WordPress development, including admin interfaces that always appear native to WordPress, I coded this framework primarily for my own uses, and it has become something that I use on all of my projects. Simply put, this saves me tons of time and headache!

As regards security, the library is completely open source, and you may always fork it on GitHub, customize to your own liking, and even create a pull request to allow others to benefit from your new feature. The code will remain public and be available to all of us. And, if you want to [suggest features](https://github.com/sergeliatko/wpsettings/issues) or [take part in feature votes or simply support the project](https://www.patreon.com/sergeliatko), you're most welcome to do so.

How it works
------------

[](#how-it-works)

### WPSettings Framework

[](#wpsettings-framework)

With WPSettings Framework, adding a simple text field to General Settings in WordPress admin and all the code that sanitizes the option value in the database can be accomplished simply as follows:

- one line to load the library in your project main file:

```
require_once('path_to_wpsettings_folder/autoload.php');
```

- and the actual snippet:

```
//make use of the class once in your file
use \SergeLiatko\WPSettings\Setting;
//then create setting like this
$my_option = new Setting( array(
	'option' => 'option_name_in_db',
	'label'  => __( 'My option label', 'my-text-domain' )
) );
```

...and that's it. Seriously, that's all the code needed to make it happen.

### WordPress Settings API

[](#wordpress-settings-api)

Now count the lines of PHP necessary to get the same (cleanly registered and properly sanitized) result for the same option using the standard WordPress Settings API:

```
add_action( 'admin_init', function () {
	register_setting(
		'general',
		'option_name_in_db',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
}, 10, 0 );

add_action( 'admin_menu', function () {
	add_settings_field(
		'option-name-in-db',
		__( 'My option label', 'my-text-domain' ),
		function() {
			printf(
				'',
				'text',
				'option-name-in-db',
				'option_name_in_db',
				esc_attr( get_option( 'option_name_in_db', '' ) ),
				'regular-text code'
			);
		},
		'general',
		'default',
		array(
			'label_for' => 'option-name-in-db'
		)
	);
}, 10, 0 );
```

**6 lines** with WPSettings framework vs **30 lines** using WordPress Settings API. Sure, if you add more options - not all of them will take 30 lines, but still it is easily 5 times faster in coding and eliminates the huge undertaking of learning the WordPress Settings API’s hidden tips and tricks.

The best part
-------------

[](#the-best-part)

WPSettings Framework takes care of:

- Adding single or multiple admin pages and/or submenu pages as well as their introductory texts to WordPress admin area.
- Adding single or multiple settings sections to existing or your custom admin pages.
- Adding setting fields to existing or your custom settings sections:
    - text inputs (all common types: hidden, text, url, email, password, tel, number, range, date etc.).
    - checkboxes (single and multiples) and radio buttons (again single and multiple).
    - text areas.
    - dropdowns (allows option groups as well).
    - any custom coded field you want (you will be surprised just how flexible the framework is).
- Adding descriptions to your custom settings sections and help messages to your setting fields.
- Registration of your options in the WordPress database.
- Sanitizing the user data input for most of the option types (you may use your own sanitize functions when needed).
- Proper handling of the option default values - does not save the defaults in the database, makes sure it is returned when no value is provided, allows forcing the default value if the user missed the value input (very useful for text options).

WPSettings Framework allows you to rewrite any functionality of the main classes providing your own extensions.

Installation
------------

[](#installation)

### Using composer

[](#using-composer)

Install the latest version of the framework with:

```
$ composer require sergeliatko/wpsettings
```

### Using git

[](#using-git)

Install the latest version of the framework with:

```
git clone https://github.com/sergeliatko/wpsettings wpsettings
```

Install all the required libraries:

```
git clone https://github.com/sergeliatko/form-fields form-fields
```

```
git clone https://github.com/sergeliatko/html html
```

#### As a submodule

[](#as-a-submodule)

Git submodules are a powerful tool, which allows you to easily include a third-party project of your own while still treating them as two separate projects. Rather than provide an in-depth explanation of the benefits and use of submodules, it's recommended you take a moment and read through [the submodules page](http://git-scm.com/book/en/Git-Tools-Submodules) in the official Git documentation. When you're ready to dive in, the following command generates a clone of WPSettings as a submodule:

```
git submodule add https://github.com/sergeliatko/wpsettings wpsettings
```

Do not forget the required libraries:

```
git submodule add https://github.com/sergeliatko/form-fields form-fields
```

```
git submodule add https://github.com/sergeliatko/html html
```

### Manually

[](#manually)

Download zip files for all necessary libraries:

- [WPSettings](https://github.com/sergeliatko/wpsettings)
- [FormFields](https://github.com/sergeliatko/form-fields)
- [HTML](https://github.com/sergeliatko/html)

And extract them in your project resources folder.

Loading the framework classes
-----------------------------

[](#loading-the-framework-classes)

### Using composer

[](#using-composer-1)

Composer will load the framework automatically.

### Manually

[](#manually-1)

If loading the classes manually (after manual installation or after installation with git), make sure the autoload.php files in all 3 libraries are included in your project:

```
