Filter uncanny-automator

uap_option_rc_get_membership_levels

Filters membership levels before they are retrieved, allowing modification of available options.

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

Description

Fires when retrieving membership levels for the Restrict Content integration. Developers can filter the `$option` array to modify data like token support, AJAX status, target fields, endpoints, or membership level options before they are used by the integration.


Usage

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

Parameters

$option (mixed)
This parameter is not used by the hook and is a leftover from a previous implementation.

Return Value

The filtered value.


Examples

add_filter( 'uap_option_rc_get_membership_levels', 'my_custom_uap_membership_levels', 10, 1 );

/**
 * Example callback function to modify the membership levels returned by UAP.
 *
 * This function demonstrates how to conditionally alter the membership levels
 * data before it's used, for example, to disable certain levels for a specific
 * use case or add custom data to them.
 *
 * @param array $option The original array of membership level options.
 * @return array The modified array of membership level options.
 */
function my_custom_uap_membership_levels( $option ) {
	// Check if we want to specifically exclude a membership level, for instance,
	// if there's a level named "Free Tier" that we don't want to offer in this context.
	if ( isset( $option['options'] ) && is_array( $option['options'] ) ) {
		foreach ( $option['options'] as $key => $level_data ) {
			// Example: Remove a membership level if its 'name' is 'Free Tier'.
			if ( isset( $level_data['name'] ) && 'Free Tier' === $level_data['name'] ) {
				unset( $option['options'][ $key ] );
			}
			// Example: Add a custom attribute to a specific membership level.
			if ( isset( $level_data['id'] ) && 5 === (int) $level_data['id'] ) {
				$option['options'][ $key ]['custom_access_note'] = __( 'This is a premium level with special features.', 'your-text-domain' );
			}
		}
		// Re-index the array after unsetting elements if necessary,
		// though for simple iteration, it might not be strictly required
		// depending on how the option is further processed.
		$option['options'] = array_values( $option['options'] );
	}

	// Always return the modified $option array.
	return $option;
}

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/restrict-content/helpers/restrict-content-helpers.php:100

public function get_membership_levels( $label = null, $option_code = null, $args = array() ) {


		if ( null === $label ) {
			$label = esc_attr_x( 'Membership level', 'Restrict Content', 'uncanny-automator' );
		}

		if ( null === $option_code ) {
			$option_code = 'RCMEMBERSHIPLEVEL';
		}

		$token        = key_exists( 'token', $args ) ? $args['token'] : false;
		$is_ajax      = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
		$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
		$end_point    = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
		$any          = key_exists( 'any', $args ) ? $args['any'] : false;

		$options = array();

		if ( $any ) {
			$options['-1'] = esc_attr_x( 'Any membership', 'Restrict Content', 'uncanny-automator' );
		}

		if ( function_exists( 'rcp_get_membership_levels' ) ) {
			// only available in Restrict Content Pro Version 3.4+
			$levels = rcp_get_membership_levels( array( 'number' => 999 ) );

			if ( ! empty( $levels ) ) {
				foreach ( $levels as $level ) {
					$options[ $level->get_id() ] = $level->get_name();
				}
			}
		}

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			'supports_tokens' => $token,
			'is_ajax'         => $is_ajax,
			'fill_values_in'  => $target_field,
			'endpoint'        => $end_point,
			'options'         => $options,
		);

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

Scroll to Top