add_action_use_once

πŸ“… Published on

πŸ“ Last updated

Documentation

Β»

Functions

Β»

add_action_use_once

Registers an action callback that automatically removes itself the first time it fires, so it only runs once even if the hook is called multiple times in a request. Useful when responding to the first occurrence of an event during a batch process.

Parameters

ParameterTypeDescription
$hook_namestringThe WordPress action hook to register on.
$callbackcallableThe callback to run once.
$priorityintHook priority. Default 10.
$accepted_argsintNumber of arguments the callback accepts. Default 1.

Return value

void β€”

Usage

\Groundhogg\add_action_use_once( 'groundhogg/event_queue/before_process', function() {
    my_plugin_acquire_processing_lock();
} );

Here’s a pseudo code example for a hypothetical external sync integration. In this case we want to sync data to an external service after a contact is updated, but we need to make sure the update has fully completed first β€” so we defer the sync by hooking into the post-update action exactly once.

add_action( 'groundhogg/contact/pre_update', 'my_plugin_defer_external_sync' );

/**
 * Defer an external CRM sync until after the contact update completes.
 *
 * @param \Groundhogg\Contact $contact
 *
 * @return void
 */
function my_plugin_defer_external_sync( $contact ) {

    $contact_id = $contact->get_id();

    // Register a one-time action that fires after the DB update is done
    \Groundhogg\add_action_use_once( 'groundhogg/db/post_update/contact', function() use ( $contact_id ) {
        $contact = \Groundhogg\get_contactdata( $contact_id );
        my_plugin_sync_contact_to_external_crm( $contact );
    } );
}

Was this helpful?

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