Action uncanny-automator

new_event_registration

Fires after a new event registration is successfully submitted. This hook provides the registration and event IDs for further processing.

add_action( 'new_event_registration', $callback, 10, 2 );

Description

Fires after a new attendee registration is successfully processed for an event. Developers can use this hook to perform custom actions, such as sending a welcome email, updating attendee-related data, or integrating with third-party services. The registration ID and event ID are passed as arguments.


Usage

add_action( 'new_event_registration', 'your_function_name', 10, 2 );

Parameters

$registration_id (mixed)
This parameter contains the ID of the new event registration.
$event_id (mixed)
This parameter contains the unique identifier for the event registration.

Examples

add_action( 'new_event_registration', 'my_custom_event_registration_handler', 10, 2 );

/**
 * Handles new event registrations.
 *
 * This function is triggered when a new event registration occurs via the WP Event Manager.
 * It demonstrates how to access the registration and event IDs and perform custom actions,
 * such as sending a notification email to the administrator.
 *
 * @param int $registration_id The ID of the new event registration.
 * @param int $event_id The ID of the event for which the registration was made.
 */
function my_custom_event_registration_handler( $registration_id, $event_id ) {
	// Get registration details if needed
	$registration = get_post( $registration_id );

	// Get event details if needed
	$event = get_post( $event_id );

	if ( ! $registration || ! $event ) {
		return; // Exit if registration or event data is not found
	}

	// Example: Send an email notification to the site administrator
	$admin_email = get_option( 'admin_email' );
	$subject = sprintf( __( 'New Event Registration for "%s"', 'your-text-domain' ), $event->post_title );
	$message = sprintf(
		__( "A new registration has been made for the event '%s' (ID: %d).n", 'your-text-domain' ),
		$event->post_title,
		$event_id
	);
	$message .= sprintf(
		__( "Registration ID: %dn", 'your-text-domain' ),
		$registration_id
	);
	// You can further retrieve attendee details from the $registration object if available
	// (e.g., using get_post_meta($registration_id, 'attendee_email', true) if that meta key is used).

	wp_mail( $admin_email, $subject, $message );

	// Example: Log the registration for debugging purposes
	error_log( sprintf( 'New event registration detected: Registration ID %d, Event ID %d', $registration_id, $event_id ) );

	// If you needed to modify something and return it (though this is an action, not a filter)
	// You would typically not 'return' a value from an action that modifies behavior.
	// If this were a filter hook like 'my_filter_hook', you would do:
	// $new_value = 'some_modified_value';
	// return $new_value;
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/wp-event-manager/actions/wp-event-manager-register-attendee.php:150

protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
		$event_id       = $parsed[ $this->get_action_meta() ];
		$attendee_email = $parsed['ATTENDEE_EMAIL'];

		// Validate event exists
		$event = get_post( $event_id );
		if ( ! $event || 'event_listing' !== $event->post_type ) {
			// translators: %s is the event ID.
			$this->add_log_error( sprintf( esc_html_x( 'Event with ID %d does not exist or is not a valid event.', 'WP Event Manager', 'uncanny-automator' ), $event_id ) );
			return false;
		}

		// Check if WP Event Manager Registrations is active
		if ( ! class_exists( 'WPEM_Registrations' ) ) {
			$this->add_log_error( esc_html_x( 'WP Event Manager Registrations plugin is not active.', 'WP Event Manager', 'uncanny-automator' ) );
			return false;
		}

		// Validate email
		if ( ! is_email( $attendee_email ) ) {
			// translators: %s is the attendee email.
			$this->add_log_error( sprintf( esc_html_x( 'Invalid email address: %s', 'WP Event Manager', 'uncanny-automator' ), $attendee_email ) );
			return false;
		}

		// Check if user is already registered for this event
		if ( get_option( 'event_registration_prevent_multiple_registrations' ) && email_has_registered_for_event( $attendee_email, $event_id ) ) {
			// translators: %s is the attendee email.
			$this->add_log_error( sprintf( esc_html_x( 'Attendee with email %s is already registered for this event.', 'WP Event Manager', 'uncanny-automator' ), $attendee_email ) );
			return false;
		}

		// Create registration
		$registration_data = array(
			'post_type' => 'event_registration',
			// translators: %s is the event title.
			'post_title' => sprintf( esc_html_x( 'Registration for %s', 'WP Event Manager', 'uncanny-automator' ), $event->post_title ),
			'post_content' => '',
			'post_status' => 'new',
			'post_parent' => $event_id,
			'meta_input' => array(
				'_attendee_email' => $attendee_email,
				'_registration_date' => current_time( 'mysql' ),
				'_registration_status' => 'new',
			),
		);

		$registration_id = wp_insert_post( $registration_data );

		if ( is_wp_error( $registration_id ) ) {
			// translators: %s is the error message.
			$this->add_log_error( sprintf( esc_html_x( 'Failed to create registration: %s', 'WP Event Manager', 'uncanny-automator' ), $registration_id->get_error_message() ) );
			return false;
		}

		// Hydrate tokens
		$this->hydrate_tokens(
			array(
				'REGISTRATION_ID' => $registration_id,
				'ATTENDEE_EMAIL' => $attendee_email,
				'EVENT_ID' => $event_id,
				'EVENT_TITLE' => $event->post_title,
			)
		);

		// Trigger the new_event_registration hook for compatibility
		do_action( 'new_event_registration', $registration_id, $event_id );

		return true;
	}

Scroll to Top