Giter Site home page Giter Site logo

wp-option's Introduction

☔ wp-option

WordPress option with some safeguards

Packagist Dependency Version wp codecov


A simple wrapper that adds validation and enforcing consistencies when dealing with WordPress options, such as when adding, updating, and retrieving an option. It supports both *_option as well as the corresponding function for network-enable installatiopn, *_site_option functions.

Why?

WordPress option can handle various type of values including strings, booleans, arrays, and integers. However, it lacks the capability to check the type of value being stored or retrieved. The retrieved value can vary depending on the type value stored in the option, for example:

  • false returns string(0) ""
  • true returns string(1) "1"
  • 0 returns string(1) "0"
  • 1 returns string(1) "1"
  • '0' returns string(1) "0"
  • '1' returns string(1) "1"
  • null returns string(0) ""

It is the responsibility of developers to ensure that the value of the option aligns with the expected type. This library aims to help handling this situation better by enabling developers to implement validations, ensuring the correct type of values when adding, updating, and retrieving an option value.

Installation

composer require syntatis/wp-option

Usage

First, create an instance of the Option class from the library and define the schema of the options. The schema is an array of options where the key is the name of the option and the value is an array of the option's schema defining:

Schema Description Values
type The type of the option value The value can be one of the following: string, boolean, integer, float, and array.
default The default value of the option Ideally, the value should be of the same type as the type value.
use Syntatis\WP\Hook\Hook;
use Syntatis\WP\Option\Option;

$option = new Option(new Hook());
$option->setSchema([
  'wporg_custom_option' => [
    'type' => 'integer',
  ],
]);
$option->register();

After the schema defined and registered, it will ensure that the returned value of the option is of the correct type. For example, if the option value is "1" (numeric string) and the type defined in the schema for the option is integer, the value will be converted to 1 when retrieved.

add_option('wporg_custom_option', '1');
get_option('wporg_custom_option'); // int(1)

By default, when a default is not set for the option registered, the value returned will be a null, instead of a false as how WordPress handles it.

get_option('wporg_custom_option'); // null

If the option is defined in the schema, the default value will be returned instead.

use Syntatis\WP\Hook\Hook;
use Syntatis\WP\Option\Option;

$option = new Option(new Hook());
$option->setSchema([
  'wporg_custom_option' => [
    'type' => 'integer',
    'default' => 0,
  ],
]);
$option->register();

get_option('wporg_custom_option'); // int(0)

For more advanced usage, please refer to the Wiki.

Reference

wp-option's People

Contributors

dependabot[bot] avatar tfirdaus avatar

Stargazers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.