arraypress/wp-stasher

A utility for capturing and storing WordPress filter values for later use within the same request.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

pkg:composer/arraypress/wp-stasher

dev-main 2025-11-30 20:08 UTC

This package is auto-updated.

Last update: 2025-11-30 20:08:14 UTC


README

A lightweight utility for capturing WordPress filter values and making them available later in the same request.

Installation

composer require arraypress/stasher

Usage

Basic Example

// Capture all parameters from a filter
stash( 'my_plugin_filter' );

// Use the values later
$value = stashed( 'my_plugin_filter', 0 );  // First parameter
$all   = stashed( 'my_plugin_filter' );     // All parameters

Named Parameters

// Store with named parameters for readability
stash( 'affwp_get_affiliate_rate', [ 'rate', 'affiliate_id', 'type', 'reference' ] );

// Access by name
$rate = stashed( 'affwp_get_affiliate_rate', 'rate' );
$type = stashed( 'affwp_get_affiliate_rate', 'type' );

Store Only Once

// Only store the first time the filter runs
stash( 'my_filter', [], true );

Required Parameters

// Only store when specific parameters have values
stash(
    'affwp_get_affiliate_rate',
    [ 'rate', 'affiliate_id', 'type', 'reference' ],
    false,
    [ 'rate', 'reference' ]  // Both must be non-empty
);

Real World Example

// Capture the affiliate rate from an early filter
add_action( 'init', function() {
    stash(
        'affwp_get_affiliate_rate',
        [ 'rate', 'affiliate_id', 'type', 'reference' ],
        true,
        [ 'rate' ]
    );
});

// Use the rate later in a different filter
add_filter( 'affwp_calc_referral_amount', function( $amount ) {
    $rate = stashed( 'affwp_get_affiliate_rate', 'rate' );
    
    if ( $rate !== null ) {
        $amount = $amount * $rate;
    }
    
    return $amount;
});

Checking and Clearing

// Check if values exist
if ( has_stashed( 'my_filter' ) ) {
    // Use the values
}

// Clear specific filter
clear_stashed( 'my_filter' );

// Clear all stored values
clear_stashed();

Using the Class Directly

use ArrayPress\Stasher\Stasher;

Stasher::stash( 'my_filter', [ 'param1', 'param2' ] );
$value = Stasher::get( 'my_filter', 'param1' );

Stasher::has( 'my_filter' );   // Check if exists
Stasher::clear( 'my_filter' ); // Clear specific
Stasher::clear();              // Clear all
Stasher::all();                // Get all stashed values
Stasher::keys();               // List all filter names

Functions

Function Description
stash( $filter, $names, $once, $required ) Capture filter values
stashed( $filter, $key ) Get stashed values
has_stashed( $filter ) Check if filter has values
clear_stashed( $filter ) Clear stashed values

Requirements

  • PHP 7.4+
  • WordPress 5.0+

License

GPL-2.0-or-later