1. Go to this page and download the library: Download screenfeed/autowpoptions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
screenfeed / autowpoptions example snippets
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.
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<mixed>
*/
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<mixed> $values The option value.
* @return array<mixed>
*/
protected function validate_values_on_update( array $values ) {
if ( ! in_array( $values['barbaz'], $values['foobar'], true ) ) {
$values['barbaz'] = $this->default_values['barbaz'];
}
return $values;
}
}