Setting the default subscription plan by passing an URL parameter

What is the use case?

Imagine you have a subscription form with a plan selector containing several subscription plans.

If you’d like to set the default subscription plan by passing an URL parameter, like on the screenshot below, then this article is for you:



How to implement it

If you’d like to set the default subscription plan of your subscription form via an URL parameter, then you have to do the following:

  1. Pass the recurring price id in the wpfsPlan URL parameter.
  2. Implement a Wordpress filter that processes the URL parameter. 

    (Default implementation provided below)

The following subsections explain these steps in detail.

1) Pass the recurring price id in the wpfsPlan URL parameter

You have to pass the recurring id in the wpfsPlan parameter (it will select the Silver plan):

https://ExampleDomain.com/sell-recurring-services-inline/?wpfsPlan=price_1JmD2iKit80XPxkD8jLNNvVM

You can obtain the recurring price id under the “Products” menu on the Stripe dashboard, in the “Pricing” section of the product details page:

WP Full Pay - Get the recurring price id in the 'Pricing' section of the product details page

2) Implement a Wordpress filter to process the URL parameter

For the sake of security, the plugin doesn’t let anyone set the default subscription plan without your consent.

You can give consent by implementing a short piece of code to approve the plan in the form of a WordPress filter. The code needs to be added to the functions.php file of your active Wordpress theme.

This is the default implementation:


<?php
/**
 * Default implementation of the 'fullstripe_select_subscription_plan' Wordpress filter 
 * for WP Full Stripe.
 *
 * @param $selectedPlanId Plan id set by a previous filter instance
 * @param $formName Name of the form that triggered the filter
 * @param $formPlanIds An associative array of plan ids assigned to the current form
 * @param $planIdParamValue Value of the wpfsPlan URL parameter
 *
 * @return integer The id of the plan to be selected on form $formName
 */

function selectSubscriptionPlan($selectedPlanId, $formName, $formPlanIds, $planIdParamValue) {
    $resultPlanId = $selectedPlanId;

    if (in_array($planIdParamValue, $formPlanIds)) {
        $resultPlanId = $planIdParamValue;
    }

    return $resultPlanId;
}

add_filter('fullstripe_select_subscription_plan', 'selectSubscriptionPlan', 10, 4);

You can customize the code depending on your needs. For example, you can limit the default plan based on form name and recurring price id:


<?php
/**
 * Custom implementation of the 'fullstripe_select_subscription_plan' Wordpress filter 
 * for WP Full Stripe.
 * Sets the selected plan if the form name is 'subscriptionInline' and 
 * the passed plan id is either 'bronze' or 'silver'.
 *
 * @param $selectedPlanId Plan id set by a previous filter instance
 * @param $formName Name of the form that triggered the filter
 * @param $formPlanIds An associative array of plan ids assigned to the current form
 * @param $planIdParamValue Value of the wpfsPlan URL parameter
 *
 * @return integer The id of the plan to be selected on form $formName
 */

function selectSubscriptionPlan($selectedPlanId, $formName, $formPlanIds, $planIdParamValue) {
    $resultPlanId = null;

    if ( $formName === 'subscriptionInline' &&
       ( 'bronze' === $planIdParamValue || 'silver' === $planIdParamValue ) ) {
        $resultPlanId = $planIdParamValue;
    }
    
    return $resultPlanId;
}

add_filter('fullstripe_select_subscription_plan', 'selectSubscriptionPlan', 10, 4);
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us