Filter
uncanny-automator
automator_mepr_recurring_subscriptions_trigger_switch
Filters recurring subscription triggers to conditionally enable or disable them based on event data.
add_filter( 'automator_mepr_recurring_subscriptions_trigger_switch', $callback, 10, 1 );
Description
Filters whether the MemberPress recurring subscription trigger should proceed. Developers can return `false` to prevent the trigger from firing for specific recurring subscriptions, allowing fine-grained control over automation logic based on the provided event object.
Usage
add_filter( 'automator_mepr_recurring_subscriptions_trigger_switch', 'your_function_name', 10, 1 );
Parameters
-
$event(mixed) - This parameter represents the default value to determine whether the trigger should run, which can be overridden by the filter.
Return Value
The filtered value.
Examples
<?php
/**
* Prevent the recurring subscription trigger from running for specific scenarios.
*
* This filter allows you to conditionally disable the MemberPress recurring subscription trigger
* based on certain conditions. For example, you might want to prevent it from running
* if the user has a specific tag or role, or if the subscription is for a particular product.
*
* @param bool $switch The default value, which is true. Returning false will prevent the trigger.
* @param $event The event object passed by the trigger.
*
* @return bool Whether to allow the trigger to run.
*/
add_filter( 'automator_mepr_recurring_subscriptions_trigger_switch', function ( $switch, $event ) {
// Example: Prevent the trigger if the event is for a specific product ID.
// Replace '123' with the actual product ID you want to exclude.
if ( isset( $event->data['product_id'] ) && $event->data['product_id'] === 123 ) {
return false; // Prevent the trigger from running for product ID 123.
}
// Example: Prevent the trigger if the user has a specific WordPress role.
// Replace 'editor' with the desired role.
if ( isset( $event->data['user_id'] ) ) {
$user = get_userdata( $event->data['user_id'] );
if ( $user && in_array( 'editor', $user->roles ) ) {
return false; // Prevent the trigger from running for users with the 'editor' role.
}
}
// If none of the exclusion conditions are met, allow the trigger to run.
return $switch;
}, 10, 2 );
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/triggers/mp-purchaseproductrecurring.php:99
$product_id = $product->ID;
$user_id = absint( $transaction->user()->ID );
if ( 'lifetime' === (string) $product->period_type ) {
return;
}
// Flexibility to prevent running the trigger.
if ( ! apply_filters( 'automator_mepr_recurring_subscriptions_trigger_switch', true, $event ) ) {
return;
}
if ( apply_filters( 'automator_mepr_check_first_real_payment', false, $event ) ) {
$subscription = $transaction->subscription();
$is_first_real_payment = Automator()->helpers->recipe->memberpress->check_if_is_renewal_or_first_payment( $subscription );
$pm = $transaction->payment_method();