Filter uncanny-automator

uap_option_all_buddypress_users

Filters the BuddyPress users option, allowing modification of the user list before it's used.

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

Description

Filters the options array for selecting BuddyPress users. Developers can modify the user selection options, such as adding or removing users, or changing how users are displayed, before they are presented in the Uncanny Automator trigger or action. This filter fires when populating the user selection field for BuddyPress integrations.


Usage

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

Parameters

$option (mixed)
This parameter holds the option's value, which can be a mixed type depending on the specific filter implementation.

Return Value

The filtered value.


Examples

/**
 * Filter the BuddyPress user options to include only active users.
 *
 * This example demonstrates how to modify the options passed to the
 * 'uap_option_all_buddypress_users' filter to exclude inactive BuddyPress users
 * from being available as an option in Uncanny Automator recipes.
 *
 * @param array $option The original options array.
 * @return array The modified options array, excluding inactive users.
 */
add_filter( 'uap_option_all_buddypress_users', function( $option ) {

	// Check if the 'options' key exists and is an array.
	if ( ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
		return $option; // Return original if options are not in the expected format.
	}

	// Get all BuddyPress users.
	$all_users = bp_core_get_users( array( 'type' => 'active', 'per_page' => 99999 ) ); // Fetch all active users

	$filtered_options = array();

	// Iterate through the original options and compare with active BuddyPress users.
	foreach ( $option['options'] as $user_id => $user_label ) {
		$is_active_buddypress_user = false;
		if ( ! empty( $all_users['users'] ) ) {
			foreach ( $all_users['users'] as $bp_user ) {
				if ( (int) $bp_user->id === (int) $user_id ) {
					$is_active_buddypress_user = true;
					break;
				}
			}
		}

		// Only include the option if the user is an active BuddyPress user.
		if ( $is_active_buddypress_user ) {
			$filtered_options[ $user_id ] = $user_label;
		}
	}

	// Update the options in the original array.
	$option['options'] = $filtered_options;

	return $option;

}, 10, 1 ); // Priority 10, 1 accepted argument

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/buddypress/helpers/buddypress-helpers.php:155

public function all_buddypress_users( $label = null, $option_code = 'BPUSERS', $args = array() ) {

		if ( ! $label ) {
			$label = esc_attr__( 'User', 'uncanny-automator' );
		}

		$args = wp_parse_args(
			$args,
			array(
				'uo_include_any' => false,
				'uo_any_label'   => esc_attr__( 'Any user', 'uncanny-automator' ),
			)
		);

		$options = array();

		if ( Automator()->helpers->recipe->load_helpers ) {
			if ( $args['uo_include_any'] ) {
				$options[- 1] = $args['uo_any_label'];
			}

			$users = Automator()->helpers->recipe->wp_users();

			foreach ( $users as $user ) {
				$options[ $user->ID ] = $user->display_name;
			}
		}

		$option = array(
			'option_code'              => $option_code,
			'label'                    => $label,
			'input_type'               => 'select',
			'required'                 => true,
			'options'                  => $options,
			'custom_value_description' => esc_attr__( 'User ID', 'uncanny-automator' ),
		);

		return apply_filters( 'uap_option_all_buddypress_users', $option );
	}

Scroll to Top