Skip to main content

benecaster_get_secret()

Utilities Free
benecaster_get_secret( string $addon_slug, string $name ): ?string

Returns a secret an add-on stored with benecaster_store_secret(), decrypted, for use on the server: in a call to the provider it belongs to, for example.

Parameters

Name Type Default Required Description
addon_slug string — Yes Your add-on's slug, in either form (my-addon or benecaster-addon-my-addon).
name string — Yes The name the secret was stored under.

Return Value

Type: string|null

The secret, decrypted. null when none is stored, or when it can no longer be read because the key changed or the stored value was tampered with.

Example

$key = benecaster_get_secret( 'my-addon', 'api_key' );

if ( null === $key ) {
    // None stored, or it can no longer be read: ask the podcaster for it again.
    return new WP_Error( 'my_addon_no_key', __( 'Enter your API key again.', 'my-addon' ) );
}

$response = wp_remote_post( 'https://api.example.com/v1/search', [
    'headers' => [ 'Authorization' => 'Bearer ' . $key ],
] );

Notes

null means "ask the podcaster for it again", not an error. It never throws, so check for null before calling a provider. Reading never creates an encryption key.

Never send the value to the browser. To tell a settings screen whether a key is set, send null !== benecaster_get_secret( … ), not the key.

There is no capability check.

Need this built rather than just documented? See our services →