add_filter_use_once

📅 Published on

📝 Last updated

Documentation

»

Functions

»

add_filter_use_once

Registers a filter callback that automatically removes itself after being called once. The filter only modifies the value for a single pass, then deregisters itself. Replaces the deprecated add_self_removing_filter().

Parameters

ParameterTypeDescription
$filterstringThe WordPress filter hook to register on.
$callbackcallableThe callback to run once.
$priorityintHook priority. Default 10.
$argsintNumber of arguments. Default 1.

Return value

bool — true on success.

Usage

\Groundhogg\add_filter_use_once( 'groundhogg/email/subject', function( $subject ) {
return 'One-time override: ' . $subject;
} );

Example

Here’s a pseudo code example for a hypothetical per-contact subject line override.

add_action( 'groundhogg/broadcast/before', 'my_plugin_maybe_override_subject', 10, 3 );

/**
 * Override the subject line for contacts in the B variant of an A/B test.
 *
 * @param \Groundhogg\Broadcast $broadcast
 * @param \Groundhogg\Contact   $contact
 * @param \Groundhogg\Event     $event
 *
 * @return void
 */
function my_plugin_maybe_override_subject( $broadcast, $contact, $event ) {

    if ( ! $contact->has_tag( 'ab-test-group-b' ) ) {
        return;
    }

    \Groundhogg\add_filter_use_once( 'groundhogg/email/subject', function( $subject ) {
        return '[Special offer] ' . $subject;
    } );
}

Was this helpful?

Let us know if this document answered your question. That’s the only way we can improve.