Filter uncanny-automator

uap_option_wp_user_roles

Filters the available WordPress user roles for user role selection in the plugin.

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

Description

Filters the user role options array before it's displayed in Uncanny Automator's recipe settings. Developers can modify the available roles, labels, or other select field attributes to customize user role selection within automations. This hook fires during the rendering of recipe settings.


Usage

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

Return Value

The filtered value.


Examples

/**
 * Example: Add a custom role to the available user roles for Uncanny Automator.
 *
 * This function hooks into the 'uap_option_wp_user_roles' filter to modify the
 * list of available WordPress user roles that can be selected within Uncanny Automator.
 * It adds a specific custom role if it exists.
 *
 * @param array $option The original array of options for the user roles field.
 * @return array The modified array of options, potentially including a custom role.
 */
add_filter( 'uap_option_wp_user_roles', function( $option ) {

	// Define the slug of the custom role we want to add.
	$custom_role_slug = 'your_custom_role_slug'; // Replace with your actual custom role slug.

	// Check if the custom role exists in WordPress.
	if ( ! empty( get_role( $custom_role_slug ) ) ) {

		// Get the display name of the custom role.
		$custom_role_data = get_role( $custom_role_slug );
		$custom_role_label = translate_user_role( $custom_role_data->name );

		// Add the custom role to the existing roles in the 'options' array.
		// It's often good practice to add custom roles at the end or in a logical order.
		if ( isset( $option['options'] ) && is_array( $option['options'] ) ) {
			$option['options'][ $custom_role_slug ] = $custom_role_label;
		} else {
			// If 'options' doesn't exist or isn't an array, initialize it.
			$option['options'] = [ $custom_role_slug => $custom_role_label ];
		}
	}

	// Always return the modified (or unmodified) option array.
	return $option;

}, 10, 1 ); // Priority 10, accepts 1 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/wp/helpers/wp-helpers.php:291

public function wp_user_roles( $label = null, $option_code = 'WPROLE', $is_any = false ) {

		if ( ! $label ) {
			/* translators: WordPress role */
			$label = esc_attr_x( 'Role', 'WordPress', 'uncanny-automator' );
		}

		$roles = array();
		if ( true === $is_any ) {
			$roles['-1'] = esc_attr_x( 'Any role', 'WordPress', 'uncanny-automator' );
		}

		$default_role           = get_option( 'default_role', 'subscriber' );
		$roles[ $default_role ] = wp_roles()->roles[ $default_role ]['name'];

		foreach ( wp_roles()->roles as $role_name => $role_info ) {
			if ( $role_name !== $default_role ) {
				$roles[ $role_name ] = $role_info['name'];
			}
		}

		$option = array(
			'option_code'              => $option_code,
			'label'                    => $label,
			'input_type'               => 'select',
			'required'                 => true,
			'options'                  => $roles,
			'custom_value_description' => esc_attr_x( 'Role slug', 'WordPress', 'uncanny-automator' ),
		);

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


Scroll to Top