Filter uncanny-automator

uap_option_all_memberpress_products

Filters all MemberPress products to allow modification before they are displayed or used by the plugin.

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

Description

Filters the array of all available MemberPress product options, allowing developers to modify or add to the list. This hook fires when generating the options for a MemberPress product selection in Uncanny Automator, providing full control over which products are presented to users.


Usage

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

Parameters

$option (mixed)
This parameter holds the label for the product dropdown in the Uncanny Automator interface.

Return Value

The filtered value.


Examples

<?php

/**
 * Filters the MemberPress products list to exclude specific products.
 *
 * This function demonstrates how to modify the array of MemberPress products
 * passed to the 'uap_option_all_memberpress_products' filter. In this example,
 * we're removing any product that has "Test" in its label.
 *
 * @param array $option An array of MemberPress products. Each product is likely an associative array.
 * @return array The modified array of MemberPress products.
 */
add_filter( 'uap_option_all_memberpress_products', function( $option ) {

	// Check if the $option is an array and if it contains products
	if ( ! is_array( $option ) || empty( $option['options'] ) || ! is_array( $option['options'] ) ) {
		return $option; // Return original if not in expected format
	}

	$filtered_products = [];

	// Iterate through each product
	foreach ( $option['options'] as $product_key => $product_data ) {
		// Assuming each $product_data is an associative array with a 'label' key
		if ( isset( $product_data['label'] ) && strpos( $product_data['label'], 'Test' ) === false ) {
			// Keep the product if its label does not contain "Test"
			$filtered_products[ $product_key ] = $product_data;
		}
	}

	// Update the 'options' part of the $option array with the filtered products
	$option['options'] = $filtered_products;

	return $option;
}, 10, 1 );

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/memberpress/helpers/memberpress-helpers.php:106
uncanny-automator-pro/src/integrations/memberpress/helpers/memberpress-pro-helpers.php:93

public function all_memberpress_products( $label = null, $option_code = 'MPPRODUCT', $args = array() ) {

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

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

		$options = array();

		if ( $args['uo_include_any'] ) {
			$options['-1'] = $args['uo_any_label'];
		}

		$results = Automator()->helpers->recipe->options->wp_query( array( 'post_type' => 'memberpressproduct' ) );
		if ( ! empty( $results ) ) {
			foreach ( $results as $k => $v ) {
				$options[ $k ] = $v;
			}
		}

		$relevant_tokens = array(
			$option_code                => esc_attr__( 'Product title', 'uncanny-automator' ),
			$option_code . '_ID'        => esc_attr__( 'Product ID', 'uncanny-automator' ),
			$option_code . '_URL'       => esc_attr__( 'Product URL', 'uncanny-automator' ),
			$option_code . '_THUMB_ID'  => esc_attr__( 'Product featured image ID', 'uncanny-automator' ),
			$option_code . '_THUMB_URL' => esc_attr__( 'Product featured image URL', 'uncanny-automator' ),
		);

		if ( isset( $args['relevant_tokens'] ) && ! empty( $args['relevant_tokens'] ) && is_array( $args['relevant_tokens'] ) ) {
			$relevant_tokens = array_merge( $relevant_tokens, $args['relevant_tokens'] );
		}

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			'options'         => $options,
			'relevant_tokens' => $relevant_tokens,
		);

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

Scroll to Top