Filter uncanny-automator

automator_modify_user_results

Filters the user results before they are displayed or processed by the automator.

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

Description

Modify the user query results before they are cached or returned. Developers can use this filter to add, remove, or alter user data fetched for Automator recipes, offering fine-grained control over user selection. This hook fires after users are retrieved from the database.


Usage

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

Parameters

$users (mixed)
This parameter contains the list of WordPress users to be processed.

Return Value

The filtered value.


Examples

<?php
/**
 * Example: Add a custom role to users who are fetched by the Automator recipe helper.
 *
 * This filter allows you to modify the array of user objects returned by the
 * automator_recipe_helpers function before they are used elsewhere in the plugin.
 *
 * @param array $users An array of WP_User objects.
 * @return array Modified array of WP_User objects.
 */
add_filter( 'automator_modify_user_results', 'my_automator_add_custom_role_to_users', 10, 1 );

function my_automator_add_custom_role_to_users( $users ) {
    // Ensure we have an array of users to process.
    if ( ! is_array( $users ) ) {
        return $users;
    }

    $custom_role_to_add = 'your_custom_role_slug'; // Replace with your actual custom role slug

    foreach ( $users as &$user ) {
        // Check if the user object is valid and has a 'roles' property.
        if ( is_a( $user, 'WP_User' ) && isset( $user->roles ) && is_array( $user->roles ) ) {
            // Add the custom role if it's not already assigned.
            if ( ! in_array( $custom_role_to_add, $user->roles, true ) ) {
                $user->roles[] = $custom_role_to_add;
            }
        }
    }
    // Unset the reference to the user to avoid potential issues.
    unset( $user );

    return $users;
}
?>

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/core/lib/helpers/class-automator-recipe-helpers.php:1007

public function wp_users( $limit = 99999 ) {
		global $wpdb;
		// prepare transient key.
		$transient_key = 'automator_transient_users';
		// attempt fetching options from transient.
		$users = Automator()->cache->get( 'uap_transient_users' );
		if ( empty( $users ) ) {
			$query = apply_filters(
				'automator_get_users_query',
				"SELECT ID, display_name
					FROM $wpdb->users
					ORDER BY display_name
					LIMIT 0, $limit"
			);
			$users = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

			// save fetched posts in a transient for 3 minutes for performance gains.
			$expiration_time = apply_filters( 'automator_get_users_expiry_time', Automator()->cache->expires, $users );
			Automator()->cache->set( $transient_key, $users, 'automator', $expiration_time );
		}

		return apply_filters( 'automator_modify_user_results', $users );
	}


Scroll to Top