Filter
uncanny-automator-pro
automator_pro_role_change_from_to_new_role_run_on_empty
Filters whether to run Automator Pro automations when a user's role changes to an empty state.
add_filter( 'automator_pro_role_change_from_to_new_role_run_on_empty', $callback, 10, 3 );
Description
Filters whether the 'User role changed from/to' trigger should run when a user has no previous roles. Developers can use this to conditionally prevent the trigger from firing for newly created users or users with roles removed.
Usage
add_filter( 'automator_pro_role_change_from_to_new_role_run_on_empty', 'your_function_name', 10, 3 );
Parameters
-
$user_id(mixed) - This parameter is a boolean value that determines whether the trigger should run when no old roles are present for the user.
-
$role(mixed) - This parameter is the ID of the user whose role is being changed.
-
$old_roles(mixed) - This parameter specifies whether the automation should run even if the user has no previous roles.
Return Value
The filtered value.
Examples
/**
* Example of using the 'automator_pro_role_change_from_to_new_role_run_on_empty' filter.
* This filter allows you to control whether a trigger should run when a user's roles change
* and they previously had no roles assigned (i.e., their old_roles array is empty).
*
* In this example, we'll *prevent* the trigger from running if the user had no previous roles,
* unless a specific user ID is involved in the change.
*
* @param bool $run_on_empty The default value, indicating whether to run on empty old roles.
* @param int $user_id The ID of the user whose roles are changing.
* @param string $role The new role being assigned to the user.
* @param array $old_roles An array of the user's previous roles.
*
* @return bool True to run the trigger, false to prevent it.
*/
function my_automator_prevent_role_change_on_empty_roles( $run_on_empty, $user_id, $role, $old_roles ) {
// Define a specific user ID that we *always* want to trigger, even with empty old roles.
$exception_user_id = 123; // Replace with a real user ID for testing.
// If the user has no old roles AND it's not our exception user, prevent the trigger.
if ( empty( $old_roles ) && $user_id !== $exception_user_id ) {
return false; // Do not run the trigger if the user had no previous roles and is not the exception.
}
// Otherwise, respect the default behavior of the filter.
return $run_on_empty;
}
add_filter( 'automator_pro_role_change_from_to_new_role_run_on_empty', 'my_automator_prevent_role_change_on_empty_roles', 10, 4 );
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/wp/triggers/wp-userrolechangedfrmto.php:93
/**
* @param $user_id
* @param $role
* @param $old_roles
*/
public function set_user_role( $user_id, $role, $old_roles ) {
if ( empty( $old_roles ) && false === apply_filters( 'automator_pro_role_change_from_to_new_role_run_on_empty', true, $user_id, $role, $old_roles ) ) {
return;
}
$recipes = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
$required_old_role = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
$required_new_role = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta_new );