fbpx

MemberMouse WordPress Filters

Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data. MemberMouse defines its own set of filters that you can utilize to control the flow of execution in MemberMouse prior to certain actions being taken.

Here's a basic example:

function customContentProtection($data) 
{
     return true;
}
add_filter('mm_bypass_content_protection', 'customContentProtection');

In this example, we're using the add_filter() method to indicate that the  customContentProtection() method should be called when MemberMouse applies the  mm_bypass_content_protection filter.

To learn more about working with WordPress' Action API read this article.

MemberMouse Filter Reference

The following is a list of all of the MemberMouse filters, a description of when they're executed, and information on the data that will be passed to any function hooked into the filter.

Billing Scan Interval

mm_billing_scan_interval

MemberMouse 3.0+

MemberMouse periodically executes a WP-Cron task that scans for subscriptions that are due to rebill, and enqueues them for processing by the local billing system. By default, scans are performed every fifteen minutes. This filter allows you to modify the interval between scans, which may be helpful when optimizing the performance of your site.

The plugin passes a single argument $scanInterval to your function, an integer representing the default number of minutes between scans. Your function should return an integer representing the desired minutes between scans. For example, the following will scan for subscriptions that are ready to bill once every five minutes:

function customBillingScanInterval($scanInterval)
{
   return 5;
}
add_filter("mm_billing_scan_interval", "customBillingScanInterval", 10, 2);

Subscription Dunning Interval

mm_billing_subscription_dunning_interval

MemberMouse 3.0+

If a subscription fails to bill on the scheduled date, MemberMouse makes three additional attempts to collect the payment. This filter allows you to control the interval between the attempts.

MemberMouse passes two arguments to your function. First is an integer $failureInterval which represents the default number of days before the next attempt will be made. Second is an integer $failureState which represents the particular failed billing state. Three failure states currently exist: 2, 3, 4 which represent the second, third, and final attempt to collect payment, respectively.

Your function should return an integer representing the number of days that should be allowed to elapse before the next attempt. Here's an example that replicates the default dunning intervals:

function customDunningInterval($failureInterval, $failureState)
{
   switch ($failureState)
   {
      case 2:
         return 2; // First retry, two days after initial rebill failure 
      
      case 3:
         return 3; // Second retry, three days after first retry
      
      case 4:
         return 2; // Third retry, two days after second retry
      
      default:
         return $failureInterval;
   }
}
add_filter("mm_billing_subscription_dunning_interval", "customDunningInterval", 10, 2);

To customize the dunning intervals, you can simply change the values returned in the three case statements to your desired length in days.


Block Access

mm_block_access

This filter is applied after MemberMouse has determined the current visitor has access to the content, and allows you to selectively block access to protected content.

MemberMouse passes one boolean value $blockAccess to your function, indicating whether it will block access to the content by default. Your function should return a boolean indicating whether MemberMouse should block access to the content.

For example, to block all members from accessing protected content:

function blockMemberAccess($blockAccess)
{
   return true;
}
add_filter("mm_block_access", "blockMemberAccess", 10, 2);

Bypass Content Protection

mm_bypass_content_protection

This filter is applied after MemberMouse has determined the current visitor doesn't have access to the content, and allows you to selectively bypass content protection.

MemberMouse passes one boolean value $allowAccess to your function, indicating whether it will provide access to the content by default. Your function should return a boolean indicating whether MemberMouse should allow access to the content.

For example, the following will allow any members who have been assigned the Contributor role access to all protected pages and posts:

function allowAccessForContributors($allowAccess)
{
    if (current_user_can("contributor"))
    {
        return true;
    }
    else
    {
        return $allowAccess;
    }
}
add_filter("mm_bypass_content_protection", "allowAccessForContributors", 10, 2);

Please note that while this filter will provide access to pages and posts which would otherwise be inaccessible, it does not impact the parsing of Decision SmartTags.


Login Redirect

mm_login_redirect

This filter is applied prior to MemberMouse executing its default login redirect behavior which is configured in Member Homepage Settings. Return a URL to indicate that MemberMouse should redirect the user to that URL. Return an empty string to indicate that MemberMouse should use its own internal rules for determining where the user should be sent.

The object passed to your function contains the URL MemberMouse will redirect the user to if you don't return an override URL and a user object containing data about the logged in user.
If the name of the object in your function is $infoObj then you'll access the two data elements as follows:

$infoObj->currentUrl
$infoObj->user

Stripe Statement Descriptor

mm_stripe_billing_statement_descriptor

When MemberMouse initiates a rebill, it sends various descriptive data about the charge. The statement descriptor is what will be displayed to the customer on their credit card bill, and it is also visible within Stripe. This filter allows you to customize the statement descriptor for rebill transactions.

Please note that the maximum total length of the statement descriptor is 22 characters, and sending a longer string will result in failure to process the payment. Also, the data you send may be truncated. Stripe ultimately creates a “calculated statement descriptor” by appending the descriptor you send to the Shortened Descriptor specified in Stripe Dashboard Public Settings, but the maximum length of this calculated string is also 22 characters, and anything beyond this will be discarded.

To calculate the maximum length of the content will be displayed, use the following formula:

max displayed length = 22 – (length of shortened descriptor + 2)

MemberMouse will pass two parameters to your function. The first is $descriptor which is a string containing the information MemberMouse will send unless modified. The second is $order which is a standard class object that contains MemberMouse order data, which you may use in constructing the statement descriptor. 

Here's an example: 

add_filter("mm_stripe_billing_statement_descriptor", function($descriptor, $order)
{

   $descriptor = "Order #: " . $order->orderNumber;
   $maxLengthDescriptor = substr($descriptor,0,22);
   return $maxLengthDescriptor;
}, 1, 2);

If your Shortened Descriptor is ABC your customers should see something like on their credit card statements:

ABC* Order #: 10043

This filter can only act on rebills, not initial payments.


Stripe PaymentIntent Description

mm_stripe_paymentintent_description

Another piece of data sent to Stripe with rebills is the Description, which this filter allows you to customize. The Description is one of the columns that is displayed when you select Payments in Stripe Dashboard. The maximum length of this field is 350 characters.

MemberMouse will pass two parameters to your function. The first is $description which is a string containing the information MemberMouse will send unless modified. The second is $order which is a standard class object that contains MemberMouse order data, which you may use in constructing the Description. 

For example, to set this field to Order# 1234, Item: Digital Membership you could use the following function in your plugin:

add_filter('mm_stripe_paymentintent_description', function($description, $order)
{
   $description = "Order # " . $order->orderNumber;
   if (isset($order->orderProducts) && is_array($order->orderProducts))
   {
      $description .= ", Item: " . array_pop($order->orderProducts)->description;
   }
   return $description;
}, 1, 2);

This filter can only act on rebills, not initial payments. To see all the data that's possible to access on the $order object, view this sample order object.


Default MemberMouse Filters

These are filters that are included in the plugin that can be overridden in your custom code using remove_filter to implement your own related filters.

Password Strength Validator

mm_password_strength_validator

As of version 2.2.9, MemberMouse supports the addition of a custom password validation filter. This allows the password to be evaluated based on custom requirements. By default MemberMouse attaches a basic filter that checks to ensure the password is at least 8 characters. Developers have the option to add an additional filter that will be applied in conjunction with the default, or remove the default and replace it with a custom filter.

The example below shows the steps to remove the default filter and replace it with a custom filter. This can be added into the theme's function.php file, but we recommended that if you take this route, you use a Child Theme to avoid your code being lost in the event of a theme update.

//remove the default filter 
remove_filter('mm_password_strength_validator',array('MM_UserHooks',"passwordStrengthValidator"));

//add a filter that enforces that passwords are at least 8 characters and contain an uppercase letter, a number, and a special character 
add_filter('mm_password_strength_validator', function($passwordData)
{
   if (is_object($passwordData) && isset($passwordData->data) && !preg_match('/^(?=.*[!@#$%^&*-])(?=.*[0-9])(?=.*[A-Z]).{8,20}$/', $passwordData->data))
   {
      $passwordData->type     = "error";
      $passwordData->errors[] = "Password must contain an uppercase character, a special character, and a number";
   }
   return $passwordData;
}, 10, 1);
Was this article helpful?

Related Articles