PHPackages                             screenfeed/autowpoptions - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. screenfeed/autowpoptions

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

screenfeed/autowpoptions
========================

Manage a set of WordPress options.

v1.0.1(5y ago)122[1 PRs](https://github.com/Screenfeed/autowpoptions/pulls)1GPL-2.0PHPPHP &gt;=5.4.0

Since Jan 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Screenfeed/autowpoptions)[ Packagist](https://packagist.org/packages/screenfeed/autowpoptions)[ Docs](https://github.com/Screenfeed/autowpoptions)[ RSS](/packages/screenfeed-autowpoptions/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (2)Dependencies (10)Versions (5)Used By (1)

AutoWPOptions
=============

[](#autowpoptions)

Allows to manage a set of options in WordPress.

Requires **php 5.4** and **WordPress 4.4**.

What you will be able to do
---------------------------

[](#what-you-will-be-able-to-do)

- Decide if your set of options is network-wide or site-wide in a multisite environment,
- Obviously, get/set/delete values,
- Provide default and reset values,
- Cast, sanitize, and validate values automatically,
- Create an upgrade process.

How to install
--------------

[](#how-to-install)

With composer:

```
"require": {
	"screenfeed/autowpoptions": "*"
},
```

How to use
----------

[](#how-to-use)

Create one class that extends *Sanitization\\AbstractSanitization*. This class will contain the following:

### Default values

[](#default-values)

**Required**. They are used when an option has no value yet. Their type is used to decide how to cast the values.

### Reset values

[](#reset-values)

**Optional**. Reset values are used if the whole set of options does not exist yet: sometimes you may want them to be different from the default values. Default values are used for the missing reset values.
You could also use them in a "Reset Options" process for example.

### A sanitization method

[](#a-sanitization-method)

**Required**. It is run for each option, when getting/updating it.

### A validation method

[](#a-validation-method)

**Required** (but can simply return the entry if not needed). It is run once for all options on update. It allows to edit some values, depending on others for example.

### Two keywords as class properties

[](#two-keywords-as-class-properties)

**Required**. They are used in hook names.

### Example

[](#example)

How to create an option that is stored as an array in the WordPress' options table:

- The option name is `myplugin_settings`,
- The current plugin version is `2.3`,
- The option must be network-wide on a multisite install.

```
use Screenfeed\AutoWPOptions\Storage\WpOption;
use Screenfeed\AutoWPOptions\Options;

$option_name    = 'myplugin_settings';
$network_wide   = true;
$plugin_version = '2.3';

$options_sanitization = new MyOptionsSanitization( $plugin_version );
$options_storage      = new WpOption( $option_name, $network_wide );
$options              = new Options( $options_storage, $options_sanitization );

$foobar = $options->get( 'foobar' ); // Returns an array of positive integers.
```

The `MyOptionsSanitization` class:

- The two keywords `myplugin` and `settings` are used in hook names, like the filter `get_myplugin_settings_foobar`.

```
use Screenfeed\AutoWPOptions\Sanitization\AbstractSanitization;

class OptionSanitization extends AbstractSanitization {

	/**
	 * Prefix used in hook names.
	 *
	 * @var string
	 */
	protected $prefix = 'myplugin';

	/**
	 * Suffix used in hook names.
	 *
	 * @var string
	 */
	protected $identifier = 'settings';

	/**
	 * The default values.
	 * These are the "zero state" values.
	 * Don't use null as value.
	 *
	 * @var array
	 */
	protected $default_values = [
		'foobar' => [],
		'barbaz' => 0,
	];

	/**
	 * Sanitizes and validates an option value. Basic casts have been made.
	 *
	 * @param  string $key     The option key.
	 * @param  mixed  $value   The value.
	 * @param  mixed  $default The default value.
	 * @return mixed
	 */
	protected function sanitize_and_validate_value( $key, $value, $default ) {
		switch ( $key ) {
			case 'foobar':
				return is_array( $value ) ? array_unique( array_map( 'absint', $value ) ) : [];
			case 'barbaz':
				return absint( $value );
		}

		return false;
	}

	/**
	 * Validates all options before storing them. Basic sanitization and validation have been made, row by row.
	 *
	 * @param  array $values The option value.
	 * @return array
	 */
	protected function validate_values_on_update( array $values ) {
		if ( ! in_array( $values['barbaz'], $values['foobar'], true ) ) {
			$values['barbaz'] = $this->default_values['barbaz'];
		}
		return $values;
	}
}
```

### An "upgrade process"?

[](#an-upgrade-process)

The plugin version used when instanciating `MyOptionsSanitization` is stored in the option and can be used in a future plugin release for an upgrade process.
For example:

```
$site_version = $options->get( 'version' );

if ( version_compare( $site_version, '1.2' ) < 0 ) {
	$options->set( [ 'barbaz', 8 ] );
}

$options->set( [ 'version', '2.5' ] );
```

### Reserved keyworks

[](#reserved-keyworks)

Don't use the following keywords as option keys, they are used internally:

- cached
- version

Extending
---------

[](#extending)

You may want to store your options elsewhere than the WordPress' options table, maybe in configuration file (heck, why not). This package is built in such a way that it is possible.
To do so, you need to create a class that will replace `Screenfeed\AutoWPOptions\Storage\WpOption`, and implement `Screenfeed\AutoWPOptions\Storage\StorageInterface`.

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~21 days

Total

2

Last Release

1976d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1878479?v=4)[Grégory Viguier](/maintainers/Screenfeed)[@Screenfeed](https://github.com/Screenfeed)

---

Top Contributors

[![Screenfeed](https://avatars.githubusercontent.com/u/1878479?v=4)](https://github.com/Screenfeed "Screenfeed (13 commits)")

---

Tags

default-valuesoptionsreset-valuessanitizationvalidationwordpresswordpressoptions

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/screenfeed-autowpoptions/health.svg)

```
[![Health](https://phpackages.com/badges/screenfeed-autowpoptions/health.svg)](https://phpackages.com/packages/screenfeed-autowpoptions)
```

###  Alternatives

[devgeniem/wp-sanitize-accented-uploads

Replaces accents from future uploads and has wp-cli command which you can use to sanitize current content.

27101.7k5](/packages/devgeniem-wp-sanitize-accented-uploads)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
