Filter uncanny-automator-pro

automator_pro_pmp_admin_assigns_level_set_member_as_user

Filters whether a member should be set as a user for Paid Memberships Pro when assigned a level.

add_filter( 'automator_pro_pmp_admin_assigns_level_set_member_as_user', $callback, 10, 1 );

Description

Fires after a Paid Memberships Pro level is assigned. Developers can filter this hook to prevent the assigned member from being automatically set as the current user in Uncanny Automator, useful for specific automation scenarios or to maintain a different user context.


Usage

add_filter( 'automator_pro_pmp_admin_assigns_level_set_member_as_user', 'your_function_name', 10, 1 );

Parameters

$member_user_id (mixed)
This parameter is a boolean value that indicates whether the current user is considered a valid admin and if the subsequent logic should proceed.

Return Value

The filtered value.


Examples

/**
 * Prevent setting the assigned member as the current user if they have a specific role.
 *
 * This filter allows administrators to control whether a user assigned a membership level
 * via Uncanny Automator Pro should automatically become the "current user" for the
 * subsequent actions in the recipe. This is useful if you want to perform actions
 * as an administrator or a different user in the context of the recipe.
 *
 * @param bool     $should_set_as_user Whether to set the member as the current user.
 * @param int|null $member_user_id   The user ID of the member being assigned the level.
 *
 * @return bool The modified value for $should_set_as_user.
 */
add_filter( 'automator_pro_pmp_admin_assigns_level_set_member_as_user', function( $should_set_as_user, $member_user_id ) {
	// If the member_user_id is not available, we can't proceed, so keep the default.
	if ( ! $member_user_id ) {
		return $should_set_as_user;
	}

	// Get the user object.
	$user = get_userdata( $member_user_id );

	// Check if the user exists and has a specific role, e.g., 'administrator'.
	// If they do, prevent them from being set as the current user for the recipe.
	if ( $user && user_can( $member_user_id, 'administrator' ) ) {
		return false; // Do not set this administrator as the current user.
	}

	// Otherwise, allow the default behavior.
	return $should_set_as_user;
}, 10, 2 );

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

uncanny-automator-pro/src/integrations/paid-memberships-pro/triggers/pmp-assign-membership-level.php:105

protected function validate_trigger( ...$args ) {
		if ( ! is_admin() || ! current_user_can( 'manage_plugins' ) ) {
			return false;
		}

		$args = array_shift( $args );

		if ( ! empty( $args[2] ) ) {
			return false;
		}

		// Set member user ID as current recipe user.
		$member_user_id = $args[1];
		// Add filter to override setting member as user
		if ( apply_filters( 'automator_pro_pmp_admin_assigns_level_set_member_as_user', true, $member_user_id ) ) {
			$this->set_user_id( $member_user_id );
		}

		return true;
	}

Scroll to Top