A class for encapsulating the logic required to maintain a relationship between the database, a dismissible UI element (with an optional lifespan), and a user's desire to dismiss that UI element.
Think of this like a WordPress Transient, but without in-memory cache support, and that uses the wp_usermeta
database table instead of wp_options
.
We invented this utility to centralize and minimize the code required to execute multiple different calls and checks to the User Meta and User Options APIs inside of WordPress Core.
We use this as the engine to power if and when we should show persistent admin-area notices to logged-in WordPress users. Dismissals can be set to expire after some number of seconds. They are global by default in a multisite environment, but can be per-site. Custom values can be saved if necessary.
Include in your plugin or theme however you feel is best.
Use with a PHP use statement:
use Sandhills\Utils\Persistent_Dismissible as PD;
PD::set( [
'id' => 'sh_dismissible_promotion_xyz'
] );
$is_dismissed = PD::get( [
'id' => 'sh_dismissible_promotion_xyz'
] );
PD::delete( [
'id' => 'sh_dismissible_promotion_xyz'
] );
/**
* @param array|string $args {
* Array or string of arguments to identify the persistent dismissible.
*
* @type string $id Required. ID of the persistent dismissible.
* @type string $user_id Optional. User ID. Default to current user ID.
* @type int|string $value Optional. Value to store. Default to true.
* @type int|string $life Optional. Lifespan. Default to 0 (infinite)
* @type bool $global Optional. Multisite, all sites. Default true.
* }
*/
This project was created by (and is managed by) Sandhills Development, LLC, where we aim to craft superior experiences through ingenuity, with deep commitment to (and appreciation for) the human element.