<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=442498563201411&amp;ev=PageView&amp;noscript=1">

WordPress: the LaterPay Dynamic Access ShortCode

img-integrations-other
Topics: WordPress · Ad-Free
Experience Level: Advanced
August 7, 2019 · 3 min read

 

 

 

 

 

 

The LaterPay WordPress Plugin is one of our simplest and most powerful integration methods. With the WordPress Plugin, not only can you get set up in mere minutes, but you also have some very powerful tools at your fingertips with other plugins, WordPress shortcodes and functions.


In this article we will look into the LaterPay Dynamic Access feature, which lets you hide content on your site based on what a user has purchased through LaterPay. This is one of our most flexible features and a few of the most common use cases are:

 

  • I want to hide ads if a user has purchased a subscription
  • I want to hide the ability to comment if a user has not purchased this article
  • I want to hide my promotions and requests for contributions if a user has purchased a time pass or subscription

We will go through each of these examples below but first we need to determine if you should use a shortcode or a function.

 

If you would like to hide content within an individual page or post continue reading for information on the Dynamic Access Shortcode. If you would like to hide content in your main WordPress template so that is it applied across your entire site, click here to jump down to our section on the Dynamic Access Function

 

Using the Dynamic Access Shortcode

To hide content within your WordPress Pages or Posts, the simplest method is to use our Dynamic Access Shortcode. Check out this article if you would like to learn more about shortcodes in general, otherwise let’s jump right in with a quick example.

 

Let’s assume that I am a small online newspaper. In each of my articles, I allow the author to add a Contributions Button in their Post to help fund their journalism. However, if the reader has purchased a time pass, I don’t want them to see this button.

 

For example:

 

If the user has not purchased a time pass:

If the user has purchased a time pass:

dynamic_access_not_purchased dynamic_access_purchased

 

We have done this by simply adding the [laterpay_check_access] shortcode around the content that we want to hide as shown below. This can be done in either the visual or text editor section of the edit post page:

 

dynamic_access_post_code

 

Shortcode Details

This shortcode is made up of a few different components; we will outline each of these below. 

 

It is important to note that the closing shortcode always remains the same and has a “/” at the beginning: [/laterpay_check_access]

 

Shortcode Configuration

Behavior

[laterpay_check_access]

content to hide

[/laterpay_check_access]

This will hide the content if the user has purchased the article.

It will not distinguish between whether it has been purchased via a single purchase, time pass, or subscription.

[laterpay_check_access timepasses=”all”]

content to hide

[/laterpay_check_access]

This will hide the content if the user has an active time pass.

 

[laterpay_check_access timepasses=”18”]

content to hide

[/laterpay_check_access]

This will hide the content if the user has an active time pass with an ID of 18.

dynamic_access_timepass

[laterpay_check_access subscriptions=”all”]

content to hide

[/laterpay_check_access]

This will hide the content if the user has an active subscription.

[laterpay_check_access subscriptions=”24,25”]

content to hide

[/laterpay_check_access]

This will hide the content if the user has an active subscription with an ID of 24 or 25.

dynamic_access_subscription

[laterpay_check_access timepasses=”18” subscriptions=”24”]

content to hide

[/laterpay_check_access]

This will hide the content if the user has an active time pass with ID of 18 or subscription with an ID of 24.

Important note: In order to prevent significant query times the following combination is NOT permitted

 

[laterpay_check_access timepasses=”all” subscriptions=”all”]

content to hide

[/laterpay_check_access]

Instead, we recommend using the first example of checking if the user has purchased via any method or listing individual time passes and subscriptions in a comma delimited list.

 

As you can see, this functionality is extremely flexible and we hope you find it useful! Keep reading to learn how to perform the same behavior inside your theme using functions or contact us if you have any questions or suggestions about this feature!

 

Using the Dynamic Access Function

While the shortcode is best for simplicity, the Dynamic Access function is superior when it comes to flexibility and site-wide changes. Odds are good that if you have ads shown on your site,  these are added in your site Theme as opposed to on the individual Post. In order to hide those ads from the theme, we will need to use the Dynamic Access Function. 

 

Functions can often seem a bit intimidating, so we will walk you through a quick example and then go through the detailed configuration options.

 

In this example, we are a news site with ads displayed in the header of our Theme. We want to hide these ads for anyone who has purchased a subscription. In order to do so we will:

 

  • Navigate to Appearance > Theme Editor in our WordPress site 
  • Select “Theme Header (header.php)” from the available Theme Files listed on the right side of the page
  • Once this is selected we find the portion of the code that is inserting the ads; for simplicity, we will just use [Ads!] in this example
  • Now we insert our Dynamic Access Function to only show Ads if the user does not have a subscription. The code would look like this:

<?php

if ( method_exists( 'LaterPay_Helper_Pricing', 'check_premium_access' ) ) {

    if ( true !== LaterPay_Helper_Pricing::check_premium_access( '' , 'all' ) ) {

        ?>

        [Ads!]

        <?php

    }

}

?>

 

Let’s break this up and look at each of the pieces individually:

  1. The code in black is simply our PHP tags; in this example we assume that you are familiar with PHP. If you aren’t familiar with PHP, don’t worry! Just follow our example.
  2. The orange code: if ( method_exists( 'LaterPay_Helper_Pricing', 'check_premium_access' ) ) {...}
    is checking to make sure that the function is available. This way your site will not break if you decided to remove LaterPay in the future. This code is optional but highly recommended.
  3. The green code: if ( true !== LaterPay_Helper_Pricing::check_premium_access( '' , 'all' ) ) {...}
    This is our Dynamic Access function. Here we are saying “If this person does not have access via any subscription then show what is inside my brackets (in this case [Ads!]; the text in blue).”
  4. The blue code: [Ads!]
    As mentioned above, in this example this would be the code which shows Ads but it could be anything you want to show or hide based on a user’s access. We will go into this more below. This is not code that you would need to write but is existing functionality that you are simply wrapping in the black, orange, and green code.

If this is outside your comfort level, don’t worry! We have dedicated integration specialists who would be happy to help you get everything set up. Simply click here and one of them will respond to your email ASAP.

 

Otherwise, keep reading for a detailed guide of the different options available with this function as well as a few more examples to help you get started.

 

Function Details

This function is extremely flexible! Below we will provide an overview of the configuration options available but as with any code, the options are virtually limitless, so do not feel confined to the examples we describe below. 

 

To save some space, we will focus only on the “green code” we went through above.

 

Function Configuration

Behavior

Provide Dynamic Access if a User has Purchased Content Using any Method

if ( true !== LaterPay_Helper_Pricing::check_premium_access

()

 ) {

}

This will show the content if the user has not purchased the article. It will not distinguish between if it has been purchased via a single purchase, time pass, or subscription.

 

For example, show a promotional pop-up if the article has not been purchased.

if ( true === LaterPay_Helper_Pricing::check_premium_access

()

 ) {

}

This will show the content if the user has purchased the article.

In this scenario we want to show a feature only if a user has purchased the article. For example, you could only show the ability to comment on a post if user has purchased that post.

Provide Dynamic Access if a User has Purchased a Subscription

if ( true !== LaterPay_Helper_Pricing::check_premium_access

( '','all' )

 ) {

}

This will show the content if the user has not purchased any subscription.

if ( true !== LaterPay_Helper_Pricing::check_premium_access

( '','24,25' )

 ) {

}

This will show the content if the user has not purchased a subscription with an ID of 24 or 25.

dynamic_access_subscription

Provide Dynamic Access if a User has Purchased a Time Pass

if ( true !== LaterPay_Helper_Pricing::check_premium_access

( 'all','')

 ) {

}

This will show the content if the user does not have access to this particular article via a time pass.

if ( true !== LaterPay_Helper_Pricing::check_premium_access

( '18','' )

 ) {

}

This will show the content if the user has not purchased a time pass with an ID of 18.

Provide Dynamic Access if a User has Purchased a Time Pass OR Subscription

if ( true !== LaterPay_Helper_Pricing::check_premium_access

( '18','24' )

 ) {

}

This will show the content if the user has not purchased a time pass with ID of 18 or a subscription with an ID of 24.

Important note: In order to prevent significant query times the following combination is NOT permitted

if ( true !== LaterPay_Helper_Pricing::check_premium_access

( 'all','all' )

 ) {

}

Instead, we recommend using the first example of checking if the user has purchased via any method or listing individual time passes and subscriptions in a comma delimited list.


If your shortcode or function is not working like you would expect or if you have a suggestion, please click here to contact LaterPay support; we're always happy to help!