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
| Parameter | Type | Description |
|---|---|---|
$hook_name | string | The WordPress action hook to register on. |
$callback | callable | The callback to run once. |
$priority | int | Hook priority. Default 10. |
$accepted_args | int | Number 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.
