PHPackages                             typisttech/wp-option-store - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. typisttech/wp-option-store

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

typisttech/wp-option-store
==========================

Extending WordPress Options API, read options from places other than database, the OOP way

0.2.0(8y ago)1517.3k↓50%2[6 issues](https://github.com/typisttech/wp-option-store/issues)[2 PRs](https://github.com/typisttech/wp-option-store/pulls)2GPL-2.0-or-laterPHPPHP &gt;7.1

Since Oct 16Pushed 5y ago1 watchersCompare

[ Source](https://github.com/typisttech/wp-option-store)[ Packagist](https://packagist.org/packages/typisttech/wp-option-store)[ Docs](https://typist.tech/projects/wp-option-store)[ RSS](/packages/typisttech-wp-option-store/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (6)Used By (2)

WP Option Store
===============

[](#wp-option-store)

[![Packagist](https://camo.githubusercontent.com/5b4cf888b91760503c8d5e8f02fd0506854840137d8bc21d66f13c251d8f3e19/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f747970697374746563682f77702d6f7074696f6e2d73746f72652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/typisttech/wp-option-store)[![Packagist](https://camo.githubusercontent.com/5df6e6a2dcba4648372228ffbff3c5aa9ae49c12c2cee408905ef3f89a70cd4b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f747970697374746563682f77702d6f7074696f6e2d73746f72652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/typisttech/wp-option-store)[![PHP from Packagist](https://camo.githubusercontent.com/609d66c12da71c1410b085f08ea11764726678947f81b91d1ceab4a3aa10e8b2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f547970697374546563682f77702d6f7074696f6e2d73746f72653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/609d66c12da71c1410b085f08ea11764726678947f81b91d1ceab4a3aa10e8b2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f547970697374546563682f77702d6f7074696f6e2d73746f72653f7374796c653d666c61742d737175617265)[![CircleCI](https://camo.githubusercontent.com/0394070dcc5ce2efe245dbbb45a21b024937e8bf7a75d015bb0efc0659aa6dea/68747470733a2f2f636972636c6563692e636f6d2f67682f547970697374546563682f77702d6f7074696f6e2d73746f72652e7376673f7374796c653d737667)](https://circleci.com/gh/TypistTech/wp-option-store)[![codecov](https://camo.githubusercontent.com/8b91c97edc188a4d61062d5996812ff7497863f77a349519d08736f1d5d477ed/68747470733a2f2f636f6465636f762e696f2f67682f547970697374546563682f77702d6f7074696f6e2d73746f72652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/TypistTech/wp-option-store)[![GitHub](https://camo.githubusercontent.com/ca48ef668bda409db1d81abeb47677df8e9a353d66ddbdda0308ba6aafd8e7bd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f547970697374546563682f77702d6f7074696f6e2d73746f72652e7376673f7374796c653d666c61742d737175617265)](https://github.com/TypistTech/wp-option-store/blob/master/LICENSE.md)[![GitHub Sponsor](https://camo.githubusercontent.com/9c923c0024fc20fbd1ea153035ab2b5bb25cf1423c5aa07c35fd3e16b227a307/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53706f6e736f722d4769744875622d6561346161613f7374796c653d666c61742d737175617265266c6f676f3d676974687562)](https://github.com/sponsors/TangRufus)[![Sponsor via PayPal](https://camo.githubusercontent.com/117a21252fdae389ecc103138cd11218cd7587caf3901b042dfe9dbe3690294e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53706f6e736f722d50617950616c2d626c75652e7376673f7374796c653d666c61742d737175617265266c6f676f3d70617970616c)](https://typist.tech/donate/wp-option-store/)[![Hire Typist Tech](https://camo.githubusercontent.com/8dd68fe769812bdfb1c34fd397ff5e022170342b1d3a47c53055c566ffe8489d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f486972652d547970697374253230546563682d6666363962342e7376673f7374796c653d666c61742d737175617265)](https://typist.tech/contact/)[![Twitter Follow @TangRufus](https://camo.githubusercontent.com/a4aa54a559719fc76a4677ca1d7ec33443d14423085acb58d268fe77f09c61ce/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f54616e6752756675733f7374796c653d666c61742d73717561726526636f6c6f723d316461316632266c6f676f3d74776974746572)](https://twitter.com/tangrufus)

Extending WordPress Options API, read options from places other than database, the OOP way.

- [The Goals, or What This Package Does?](#the-goals-or-what-this-package-does)
- [Install](#install)
- [Usage](#usage)
    - [Example](#example)
    - [ConstantStrategy](#constantstrategy)
    - [DatabaseStrategy](#databasestrategy)
    - [OptionStore](#optionstore)
        - [`__construct(StrategyInterface ...$strategies)`](#__constructstrategyinterface-strategies)
        - [`get(string $optionName)`](#getstring-optionname)
    - [Type casting](#type-casting)
    - [FilteredOptionStore](#filteredoptionstore)
        - [`get(string $optionName)`](#getstring-optionname-1)
    - [Factory](#factory)
        - [`build(): FilteredOptionStore`](#build-filteredoptionstore)
- [Frequently Asked Questions](#frequently-asked-questions)
    - [Can I implement my own strategy classes?](#can-i-implement-my-own-strategy-classes)
    - [Can I change the order of the strategies?](#can-i-change-the-order-of-the-strategies)
    - [Is this a plugin?](#is-this-a-plugin)
    - [What to do when wp.org plugin team tell me to clean up the `vendor` folder?](#what-to-do-when-wporg-plugin-team-tell-me-to-clean-up-the-vendor-folder)
    - [Can two different plugins use this package at the same time?](#can-two-different-plugins-use-this-package-at-the-same-time)
    - [Do you have real life examples that use this package?](#do-you-have-real-life-examples-that-use-this-package)
    - [Will you add support for older PHP versions?](#will-you-add-support-for-older-php-versions)
    - [It looks awesome. Where can I find some more goodies like this?](#it-looks-awesome-where-can-i-find-some-more-goodies-like-this)
    - [Where can I give ⭐⭐⭐⭐⭐ reviews?](#where-can-i-give-starstarstarstarstar-reviews)
- [Sponsoring ❤️](#sponsoring-heart)
    - [GitHub Sponsors Matching Fund](#github-sponsors-matching-fund)
    - [Why don't you hire me?](#why-dont-you-hire-me)
    - [Want to help in other way? Want to be a sponsor?](#want-to-help-in-other-way-want-to-be-a-sponsor)
- [Running the Tests](#running-the-tests)
- [Feedback](#feedback)
- [Change log](#change-log)
- [Security](#security)
- [Credits](#credits)
- [License](#license)

The Goals, or What This Package Does?
-------------------------------------

[](#the-goals-or-what-this-package-does)

WordPress Option API only allows you `get_option` from database. This package is for those who not accepting the status quo.

Install
-------

[](#install)

Installation should be done via composer, details of how to install composer can be found at .

```
$ composer require typisttech/wp-option-store
```

You should put all `WP Option Store` classes under your own namespace to avoid class name conflicts.

- [imposter-plugin](https://github.com/Typisttech/imposter-plugin)
- [mozart](https://github.com/coenjacobs/mozart)

Usage
-----

[](#usage)

### Example

[](#example)

```
use TypistTech\WPOptionStore\Factory;

// By default, the `Factory` adds 2 strategies (order matters):
//  1. ConstantStrategy
//  2. DatabaseStrategy
$filteredOptionStore = Factory::build();

// To get an option from strategies:
//  1. Read `MY_OPTION` constant
//  2. If (1) is not `null`, jump to (6)
//  3. Read from `get_option('my_option')`, the normal WordPress Options API
//  4. If (3) is not `null`, jump to (6)
//  5. We have tried all strategies, pass `null` to (6)
//  6. Pass whatever we have read (could be null) through `apply_filters('my_option', $whateverWeHaveRead);`
$filteredOptionStore->get('my_option');

// To get an option and perform type cast.
$filteredOptionStore->getBoolean('my_boolean_option');
$filteredOptionStore->getInt('my_integer_option');
$filteredOptionStore->getString('my_string_option');
$filteredOptionStore->getArray('my_array_option');
```

### ConstantStrategy

[](#constantstrategy)

This strategy gets options from [PHP constants](http://php.net/manual/en/function.constant.php).

```
define('MY_OPTION', 'abc123');

$strategy = new ConstantStrategy();

$value1 = $strategy->get('my_option');
// $value1 === 'abc123';

$value2 = $strategy->get('my_non_exist_option');
// $value2 === null;
```

### DatabaseStrategy

[](#databasestrategy)

This strategy gets options from [WordPress Options API](https://codex.wordpress.org/Options_API).

```
update_option('my_option', 'abc123');

$strategy = new DatabaseStrategy();

$value1 = $strategy->get('my_option');
// $value1 === 'abc123';

$value2 = $strategy->get('my_non_exist_option');
// $value2 === null;
```

**Important**: An unset value should be `null` instead of WordPress' default `false`.

### OptionStore

[](#optionstore)

This class gets option values from strategies.

#### `__construct(StrategyInterface ...$strategies)`

[](#__constructstrategyinterface-strategies)

`OptionStore` constructor.

- @param StrategyInterface\[\] ...$strategies Strategies that get option values.

```
$databaseStrategy = new DatabaseStrategy();
$constantStrategy = new ConstantStrategy();
$optionStore = new OptionStore($constantStrategy, $databaseStrategy);
```

Note: Strategies order matters!

#### `get(string $optionName)`

[](#getstring-optionname)

Get an option value.

- @param string $optionName Name of option to retrieve.

```
// It returns the first non-null value from strategies.
define('MY_OPTION', 'abc');
update_option('my_option', 'xyz');

$value1 = $optionStore->get('my_option');
// $value1 === 'abc';

// It returns `null` when option not found.
$value2 = $optionStore->get('my_non_exist_option');
// $value2 === null;
```

### Type casting

[](#type-casting)

`OptionStore` provides several helper methods for type casting.

```
$optionStore->getBoolean('my_boolean_option');
$optionStore->getInt('my_integer_option');
$optionStore->getString('my_string_option');
$optionStore->getArray('my_array_option');
```

### FilteredOptionStore

[](#filteredoptionstore)

This is a subclass of `OptionStore`.

#### `get(string $optionName)`

[](#getstring-optionname-1)

Get an option value.

- @param string $optionName Name of option to retrieve.

```
// It returns the first non-null value from strategies,
// and applies filters.
define('MY_OPTION', 'abc');
update_option('my_option', 'xyz');

add_filter('my_option', function($value) {
    return 'filtered ' . $value;
});

$value = $filteredOptionStore->get('my_option');
// $value === 'filtered abc';
```

Note: Filters are applied before type casting.

### Factory

[](#factory)

Factory is a helper class to reduce boilerplate code for those who use default strategies. If you use a [custom strategy](#can-i-implement-my-own-strategy-classes) or [reorder the strategies](#can-i-change-the-order-of-the-strategies), don't use this class.

#### `build(): FilteredOptionStore`

[](#build-filteredoptionstore)

```
$filteredOptionStore = Factory::build();
```

Frequently Asked Questions
--------------------------

[](#frequently-asked-questions)

### Can I implement my own strategy classes?

[](#can-i-implement-my-own-strategy-classes)

Of course! Just implements the `StrategyInterface`.

Take a look at classes `ConstantStrategy` and `DatabaseStrategy` as well as their tests for example implementations of `StrategyInterface`.

If you'd like to create a open-source package to do this to help others, open a [new issue](https://github.com/TypistTech/wp-option-store/issues/new) to let us know, we'd love to help you with it.

### Can I change the order of the strategies?

[](#can-i-change-the-order-of-the-strategies)

Why not? Don't use `Factory`.

```
// Order matters!
$optionStore = new FilteredOptionStore(
    new MyStrategy1(),
    new MyStrategy2(),
    new MyStrategy3(),
);
```

### Is this a plugin?

[](#is-this-a-plugin)

No, this is a package that should be part of your plugin.

### What to do when wp.org plugin team tell me to clean up the `vendor` folder?

[](#what-to-do-when-wporg-plugin-team-tell-me-to-clean-up-the-vendor-folder)

Re-install packages via the following command. This package exports only necessary files to `dist`.

```
$ composer install --no-dev --prefer-dist --optimize-autoloader
```

### Can two different plugins use this package at the same time?

[](#can-two-different-plugins-use-this-package-at-the-same-time)

Yes, if put all `WP Option Store` classes under your own namespace to avoid class name conflicts.

- [imposter-plugin](https://github.com/Typisttech/imposter-plugin)
- [mozart](https://github.com/coenjacobs/mozart)

### Do you have real life examples that use this package?

[](#do-you-have-real-life-examples-that-use-this-package)

Here you go:

- [Sunny](https://github.com/Typisttech/sunny)
- [WP Cloudflare Guard](https://github.com/TypistTech/wp-cloudflare-guard)

*Add your own plugin [here](https://github.com/TypistTech/wp-option-store/edit/master/README.md)*

### Will you add support for older PHP versions?

[](#will-you-add-support-for-older-php-versions)

Never! This plugin will only work on [actively supported PHP versions](https://secure.php.net/supported-versions.php).

Don't use it on **end of life** or **security fixes only** PHP versions.

### It looks awesome. Where can I find some more goodies like this?

[](#it-looks-awesome-where-can-i-find-some-more-goodies-like-this)

- Articles on Typist Tech's [blog](https://typist.tech)
- More projects on [Typist Tech's GitHub profile](https://github.com/TypistTech/)
- More plugins on [TangRufus'](https://profiles.wordpress.org/tangrufus/#content-plugins) wp.org profiles
- Stay tuned on [Typist Tech's newsletter](https://typist.tech/go/newsletter)
- Follow [@TangRufus](https://twitter.com/tangrufus) on Twitter
- Hire [Tang Rufus](https://typist.tech/contact) to build your next awesome site

### Where can I give ⭐⭐⭐⭐⭐ reviews?

[](#where-can-i-give-starstarstarstarstar-reviews)

Thanks! Glad you like it. It's important to let my know somebody is using this project. Since this is not hosted on wordpress.org, please consider:

- tweet something good with mentioning [@TangRufus](https://twitter.com/tangrufus)
- ⭐ star this [Github repo](https://github.com/typisttech/wp-option-store)
- 👀 [watch](https://github.com/typisttech/wp-option-store/subscription) this Github repo
- write blog posts
- submit [pull requests](https://github.com/typisttech/wp-option-store)
- [sponsor](https://github.com/sponsors/TangRufus) Tang Rufus to maintain his open source projects
- hire [Tang Rufus](https://typist.tech/contact) to build your next awesome site

Sponsoring ❤️
-------------

[](#sponsoring-heart)

Love `WP Option Store`? Help me maintain it, a [sponsorship here](https://typist.tech/donation/) can help with it.

### GitHub Sponsors Matching Fund

[](#github-sponsors-matching-fund)

Do you know [GitHub is going to match your sponsorship](https://help.github.com/en/github/supporting-the-open-source-community-with-github-sponsors/about-github-sponsors#about-the-github-sponsors-matching-fund)?

[Sponsor now via GitHub](https://github.com/sponsors/TangRufus) to double your greatness.

### Why don't you hire me?

[](#why-dont-you-hire-me)

Ready to take freelance WordPress jobs. Contact me via the contact form [here](https://typist.tech/contact/) or, via email

### Want to help in other way? Want to be a sponsor?

[](#want-to-help-in-other-way-want-to-be-a-sponsor)

Contact: [Tang Rufus](mailto:tangrufus@gmail.com)

Running the Tests
-----------------

[](#running-the-tests)

Run the tests:

```
$ composer test
$ composer style:check
```

Feedback
--------

[](#feedback)

**Please provide feedback!** We want to make this project useful in as many projects as possible. Please submit an [issue](https://github.com/TypistTech/wp-option-store/issues/new) and point out what you do and don't like, or fork the project and make suggestions. **No issue is too small.**

Change log
----------

[](#change-log)

Please see [CHANGELOG](./CHANGELOG.md) for more information on what has changed recently.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

[`WP Option Store`](https://github.com/TypistTech/wp-option-store) is a [Typist Tech](https://typist.tech) project and maintained by [Tang Rufus](https://twitter.com/Tangrufus), freelance developer for [hire](https://typist.tech/contact/).

Full list of contributors can be found [here](https://github.com/TypistTech/wp-option-store/graphs/contributors).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](./LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance12

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity52

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 ~75 days

Total

3

Last Release

2985d ago

PHP version history (2 changes)0.1.0PHP &gt;7.0

0.2.0PHP &gt;7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/c1b05c8ed4ea3f68173555264d0226d5faeb7f315ed9df91890c351ef576ce72?d=identicon)[TangRufus](/maintainers/TangRufus)

---

Top Contributors

[![tangrufus](https://avatars.githubusercontent.com/u/2259834?v=4)](https://github.com/tangrufus "tangrufus (55 commits)")

---

Tags

wordpresswordpress-developmentwordpress-php-librarywp-options-apiwordpresswp

### Embed Badge

![Health badge](/badges/typisttech-wp-option-store/health.svg)

```
[![Health](https://phpackages.com/badges/typisttech-wp-option-store/health.svg)](https://phpackages.com/packages/typisttech-wp-option-store)
```

###  Alternatives

[justcoded/wordpress-theme-boilerplate

WordPress theme boilerplate with better code structure and OOP support.

563.9k1](/packages/justcoded-wordpress-theme-boilerplate)[typisttech/wp-better-settings

A simplified OOP implementation of the WP Settings API

1634.8k3](/packages/typisttech-wp-better-settings)[balbuf/composer-wp

Manage WordPress core, plugins, and themes with composer.

173.6k](/packages/balbuf-composer-wp)[typisttech/wp-admin-notices

A simplified OOP implementation of the WordPress admin notices

141.2k](/packages/typisttech-wp-admin-notices)

PHPackages © 2026

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