Documentation

ExpireOption
in package

WordPress itself has a so-called "Transient API" which allows you to save custom results of e. g. database queries in the database `wp_options` table. While developing the plugin Real Media Library we experienced a lot of issues with Transients as there are a lot of performance and optimization plugins which clears the transients.

Note: You should not use this for huge query results as this option gets autoloaded if non-site-wide.

Tags
see
https://developer.wordpress.org/apis/handbook/transients/

Table of Contents

Constants

EXPIRE_SUFFIX  = '-expire'
TRANSIENT_MIGRATION_DISABLED  = 0
TRANSIENT_MIGRATION_NON_SITE_WIDE  = 1
TRANSIENT_MIGRATION_SITE_WIDE  = 2

Properties

$expiration  : mixed
$keepValueAfterExpire  : mixed
$name  : mixed
$siteWide  : mixed
$transientMigration  : mixed

Methods

__construct()  : mixed
C'tor.
delete()  : mixed
Delete option.
enableAutoload()  : mixed
Persist the option to database if not yet available so it gets autoloaded via `wp_load_alloptions()`.
enableTransientMigration()  : mixed
If you are migrating from a transient, use this method to allow reading the value via `get` from the transient value, when no option was found.
get()  : mixed
Get option value. Returns `$fallback` if not found or expired.
getExpiration()  : mixed
Get expiration in seconds.
getExpire()  : mixed
Get the expiration timestamp. Returns `0` if the value is not yet persisted to database.
getExpireName()  : mixed
Get "expire" option name.
getName()  : mixed
Get option name.
getTransientMigration()  : mixed
Get value of `transientMigration`.
isSiteWide()  : mixed
Get if option is site-wide.
set()  : mixed
Set option value.
setKeepValueAfterExpire()  : mixed
Setter.
handleMigration()  : mixed
Handle the migration and return the correct value.

Constants

TRANSIENT_MIGRATION_DISABLED

public mixed TRANSIENT_MIGRATION_DISABLED = 0

TRANSIENT_MIGRATION_NON_SITE_WIDE

public mixed TRANSIENT_MIGRATION_NON_SITE_WIDE = 1

TRANSIENT_MIGRATION_SITE_WIDE

public mixed TRANSIENT_MIGRATION_SITE_WIDE = 2

Properties

$keepValueAfterExpire

private mixed $keepValueAfterExpire

Methods

__construct()

C'tor.

public __construct(string $name, bool $siteWide, int $expiration[, bool $keepValueAfterExpire = false ]) : mixed
Parameters
$name : string

Your option name

$siteWide : bool

Use e. g. get_site_transient instead of get_transient

$expiration : int

Time until expiration in seconds

$keepValueAfterExpire : bool = false

If you use get(false, $respectExpire = false) it is recommend to keep the value in the database after expiration

Tags
codeCoverageIgnore

enableAutoload()

Persist the option to database if not yet available so it gets autoloaded via `wp_load_alloptions()`.

public enableAutoload() : mixed

enableTransientMigration()

If you are migrating from a transient, use this method to allow reading the value via `get` from the transient value, when no option was found.

public enableTransientMigration(int $type) : mixed
Parameters
$type : int

See class constants TRANSIENT_MIGRATION*

Tags
codeCoverageIgnore

get()

Get option value. Returns `$fallback` if not found or expired.

public get([mixed $fallback = false ][, bool $respectExpire = true ]) : mixed
Parameters
$fallback : mixed = false
$respectExpire : bool = true

If false, you will always get the value from database (useful if you need to merge from previous data)

Tags
see
https://developer.wordpress.org/reference/functions/get_site_transient/
see
https://developer.wordpress.org/reference/functions/get_transient/
see
https://developer.wordpress.org/reference/functions/get_site_option/
see
https://developer.wordpress.org/reference/functions/get_option/

getExpiration()

Get expiration in seconds.

public getExpiration() : mixed
Tags
codeCoverageIgnore

getExpire()

Get the expiration timestamp. Returns `0` if the value is not yet persisted to database.

public getExpire() : mixed

getExpireName()

Get "expire" option name.

public getExpireName() : mixed
Tags
codeCoverageIgnore

getName()

Get option name.

public getName() : mixed
Tags
codeCoverageIgnore

getTransientMigration()

Get value of `transientMigration`.

public getTransientMigration() : mixed
Tags
codeCoverageIgnore

isSiteWide()

Get if option is site-wide.

public isSiteWide() : mixed
Tags
codeCoverageIgnore

set()

Set option value.

public set(mixed $value[, int $expiration = null ]) : mixed
Parameters
$value : mixed

Do not pass null as null is automatically converted to an empty string in wp_options

$expiration : int = null
Tags
see
https://developer.wordpress.org/reference/functions/set_site_transient/
see
https://developer.wordpress.org/reference/functions/set_transient/
see
https://developer.wordpress.org/reference/functions/set_site_option/
see
https://developer.wordpress.org/reference/functions/set_option/

setKeepValueAfterExpire()

Setter.

public setKeepValueAfterExpire(bool $state) : mixed
Parameters
$state : bool
Tags
codeCoverageIgnore

handleMigration()

Handle the migration and return the correct value.

protected handleMigration(mixed $fallback) : mixed
Parameters
$fallback : mixed

        
On this page

Search results