Action uncanny-automator

automator_suremembers_after_access_grant

Fires after a user is granted access to a suremembers group, providing user ID and group details.

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

Description

Fires after SureMembers grants a user access to a group. Developers can use this hook to trigger custom automations or modify data when a user is successfully added to a SureMembers group. The hook passes the user ID and the specific group the user was added to.


Usage

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

Parameters

$user_id (mixed)
This parameter contains the ID of the user for whom access was just granted.
$group (mixed)
The user ID of the WordPress user who was granted access to a SureMembers group.

Examples

// Example callback function to handle the automator_suremembers_after_access_grant action.
// This function will be executed whenever a user is granted access to a SureMembers group.
function my_custom_suremembers_access_grant_handler( $user_id, $group ) {
    // Check if the user ID is valid.
    if ( ! $user_id || ! is_numeric( $user_id ) ) {
        return;
    }

    // Get the WordPress user object.
    $user = get_user_by( 'id', $user_id );

    // Check if the user object was retrieved successfully.
    if ( ! $user ) {
        return;
    }

    // Example: Log the access grant event.
    // In a real-world scenario, you might want to trigger emails,
    // update custom fields, or interact with other systems.
    error_log(
        sprintf(
            'User "%s" (ID: %d) was granted access to SureMembers group "%s" (ID: %s).',
            $user->user_login,
            $user_id,
            $group['name'], // Assuming $group is an array with a 'name' key. Adjust if structure differs.
            $group['id']   // Assuming $group is an array with an 'id' key. Adjust if structure differs.
        )
    );

    // Example: Add a meta field to the user to track their last group access grant.
    // This is a simplified example; you might want more sophisticated tracking.
    if ( isset( $group['id'] ) ) {
        update_user_meta( $user_id, 'last_suremembers_group_access_grant_id', $group['id'] );
        update_user_meta( $user_id, 'last_suremembers_group_access_grant_time', current_time( 'mysql' ) );
    }
}
add_action( 'automator_suremembers_after_access_grant', 'my_custom_suremembers_access_grant_handler', 10, 2 );

// Explanation:
// This code registers a callback function 'my_custom_suremembers_access_grant_handler'
// to the 'automator_suremembers_after_access_grant' action hook.
// The callback function receives the user ID and the group information as parameters.
// It performs basic validation and then logs the event to the PHP error log.
// It also demonstrates updating user meta fields as an example of further actions.
// The '10' is the priority (default), and '2' specifies that the callback
// function accepts 2 arguments ($user_id, $group).

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/suremembers/suremembers-integration.php:70

public function after_access_grant_router( $user_id, $groups ) {
		foreach ( $groups as $group ) {
			do_action( 'automator_suremembers_after_access_grant', $user_id, $group );
		}
	}


Internal Usage

Found in src/integrations/suremembers/triggers/user-added-to-group.php:39:

$this->add_action( 'automator_suremembers_after_access_grant', 10, 2 );
Scroll to Top