PHPackages                             devuri/wp-option - 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. devuri/wp-option

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

devuri/wp-option
================

A simple comprehensive interface for managing WordPress options, providing object-oriented access to get, add, update, and delete operations.

v0.3.1(2y ago)13.4kMITPHPPHP ^7.2 || ^7.4 || ^8.0CI passing

Since Feb 29Pushed 2y ago1 watchersCompare

[ Source](https://github.com/devuri/wp-option)[ Packagist](https://packagist.org/packages/devuri/wp-option)[ RSS](/packages/devuri-wp-option/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (5)Versions (4)Used By (0)

WP Option Bridge
================

[](#wp-option-bridge)

`WP Option` is designed to provide an object-oriented interface for managing WordPress options, simplifying the process of getting, adding, updating, and deleting options within WordPress. It acts as a `bridge` to the WordPress options API, enhancing code readability and maintainability.

Features
--------

[](#features)

- **Object-Oriented Approach**: Encapsulates WordPress option management in a single, cohesive class.
- **Dependency Injection**: Facilitates testing and flexibility by allowing custom functions for getting options.
- **Comprehensive Option Management**: Supports get, add, update, and delete operations for WordPress options.
- **Error Logging**: Basic error logging for input validation, aiding in debugging and issue resolution.

Requirements
------------

[](#requirements)

- PHP 7.2 or higher
- WordPress 4.7 or higher

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

[](#installation)

Install the package via Composer:

```
composer require devuri/wp-option
```

Usage
-----

[](#usage)

### Initialization

[](#initialization)

First, import the `WPOptionBridge` class and instantiate it:

```
use Urisoft\WPOptionBridge;

$optionBridge = new WPOptionBridge();
```

### Getting an Option

[](#getting-an-option)

Retrieve an option value using the `get_option` method:

```
$siteName = $optionBridge->get_option('blogname', 'Default Site Name');
```

### Adding an Option

[](#adding-an-option)

Add a new option using the `add_option` method:

```
$optionBridge->add_option('my_custom_option', 'My Custom Value');
```

### Updating an Option

[](#updating-an-option)

Update an existing option using the `update_option` method:

```
$optionBridge->update_option('my_custom_option', 'Updated Custom Value');
```

### Deleting an Option

[](#deleting-an-option)

Delete an option using the `delete_option` method:

```
$optionBridge->delete_option('my_custom_option');
```

Advanced Usage
--------------

[](#advanced-usage)

### Customizing Option Retrieval

[](#customizing-option-retrieval)

The `WPOptionBridge` class is designed with flexibility in mind, allowing developers to inject a custom function for retrieving option values. This is particularly useful for unit testing, where you might want to isolate the class from the WordPress database, or for integrating with a custom caching layer or option storage mechanism.

#### Using a Custom Callable

[](#using-a-custom-callable)

The set\_option\_getter of `WPOptionBridge` accepts a `callable` parameter that replaces the default WordPress `get_option` function. A `callable` in PHP is something that can be called as a function. This includes actual functions, static class methods, and object methods, among others.

Here's how you can utilize this feature:

```
use Urisoft\WPOptionBridge;

// Define a custom function for getting options.
// This is a simple example that mimics the get_option behavior.
$customOptionGetter = function($option_name, $default = false) {
    // Custom logic to retrieve an option value
    // For example, you might want to check a local cache first
    $value = /* your custom retrieval logic */;

    return $value !== null ? $value : $default;
};

$optionBridge = new Urisoft\WPOptionBridge();

// Set a custom option getter
$optionBridge->set_option_getter($customOptionGetter);
```

In this example, `$customOptionGetter` is a custom function defined to retrieve option values. When creating a new instance of `WPOptionBridge`, you pass this function as an argument. The class will then use this function instead of the default `get_option` WordPress function whenever `get_option` is called.

### Use Cases for a Custom Callable

[](#use-cases-for-a-custom-callable)

- **Unit Testing**: By injecting a custom function that returns predefined values, you can test the behavior of your code that uses `WPOptionBridge` without relying on a WordPress environment or database.
- **Caching**: If your application has a custom caching layer for options, you can inject a function that first checks the cache before falling back to the database.
- **Custom Storage**: For applications that store options outside of the WordPress database (like in a different database or a file), you can use this feature to integrate `WPOptionBridge` with your storage mechanism.

### Best Practices

[](#best-practices)

- Ensure your custom callable matches the expected signature: it should accept an option name and an optional default value, returning the option value if found, or the default if not.
- When using this feature for caching, make sure to handle cache invalidation appropriately to avoid stale data issues.

Customizing Option Management
-----------------------------

[](#customizing-option-management)

The class offers full flexibility in managing WordPress options by allowing you to define custom functions for option operations. This is particularly useful for scenarios like unit testing, integrating with caching systems, or using a custom storage mechanism for WordPress options.

### Setting Custom Functions

[](#setting-custom-functions)

You can set custom functions for each operation using the following methods:

- `set_option_getter(callable $function)`: Customize how options are retrieved.
- `set_option_adder(callable $function)`: Customize how options are added.
- `set_option_updater(callable $function)`: Customize how options are updated.
- `set_option_deleter(callable $function)`: Customize how options are deleted.

Each method accepts a `callable` argument, which should be a function that matches the expected signature of the corresponding WordPress function.

### Example

[](#example)

```
use Urisoft\WPOptionBridge;

$optionBridge = new WPOptionBridge();

// Custom function for retrieving options
$optionBridge->set_option_getter(function($name, $default = false) {
    // Custom logic to retrieve an option
});

// Custom function for adding options
$optionBridge->set_option_adder(function($name, $value) {
    // Custom logic to add an option
});

// Custom function for updating options
$optionBridge->set_option_updater(function($name, $value) {
    // Custom logic to update an option
});

// Custom function for deleting options
$optionBridge->set_option_deleter(function($name) {
    // Custom logic to delete an option
});
```

### Use Cases

[](#use-cases)

- **Unit Testing**: Mock the option functions to test your application logic without interacting with the database.
- **Caching**: Implement custom getters and setters that work with a caching layer to reduce database load.
- **Custom Storage**: Integrate with a custom storage system for WordPress options, such as an external database or a file-based storage system.

> leveraging these customization capabilities, `WPOptionBridge` becomes an adaptable tool that can fit into various architectures and testing environments, enhancing the modularity and testability of your WordPress projects.

Contributing
------------

[](#contributing)

Contributions are welcome! Please read our [contributing guide](CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests.

License
-------

[](#license)

The `WP Option Bridge` is open-sourced software licensed under the [MIT license](LICENSE).

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 89.7% 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 ~0 days

Total

3

Last Release

809d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8fd19f958b007ec6588d0a5ca2fe78e107edd652f286b836d36b5d1781d573a5?d=identicon)[devuri](/maintainers/devuri)

---

Top Contributors

[![devuri](https://avatars.githubusercontent.com/u/4777400?v=4)](https://github.com/devuri "devuri (26 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[webimpress/composer-extra-dependency

Composer plugin to require extra dependencies

11701.7k1](/packages/webimpress-composer-extra-dependency)[lidongyooo/laravel-idempotent

laravel idempotent

132.2k](/packages/lidongyooo-laravel-idempotent)

PHPackages © 2026

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