Functions available for use in WordPress themes or plugins
Memberful's WordPress plugin allows you to restrict access to an entire post or page through its Restrict Access meta box. If you want to protect certain elements or sections of your theme, you can use our Wordpress functions inside your theme.
We also offer WordPress functions for displaying profile information and links to Memberful benefits in your WordPress theme.
In this help doc:
- Check member permissions.
- Show profile information in WordPress themes.
- Link to downloads.
- Link to podcasts.
- Link to private RSS feeds.
Check member permissions
We've included three functions for checking if the current signed in user is subscribed to plans, owns downloads, or owns podcast feeds. Use them in your WordPress theme template files:
is_subscribed_to_memberful_plan( $slug )
has_memberful_download( $slug )
has_memberful_feed( $id )
If you pass either of the functions an array of slugs (or IDs in the case of feeds/podcasts), the function will return true if the member owns any of the downloads or podcasts feeds, or is subscribed to at least one of the plans.
The ID for a podcast can be found in the URL while editing a podcast:
ACCOUNT-URL.memberful.com/admin/feeds/63010/edit
You can also check if the given user has an active subscription to any plan by using the function is_subscribed_to_any_memberful_plan($user_id)
.
Basic example
Require a subscription to the Big Awesome plan:
<?php if ( is_subscribed_to_memberful_plan( '154-big-awesome' ) ) : ?>
Shown only to members with a subscription to the Big Awesome plan.
<?php endif; ?>
Multiple plans
Require a subscription to at least one of the listed plans:
<?php if ( is_subscribed_to_memberful_plan( array( '27-super-rad', '59-rock-on', '99-chill-out' ) ) ) : ?>
Shown to members with the Super Rad plan **OR** the Rock On plan **OR** the Chill Out plan.
<?php endif; ?>
Any plan
Require a subscription to any plan:
<?php if ( is_subscribed_to_any_memberful_plan( $user_id ) ) : ?>
Shown to members subscribed to **any** plan.
<?php endif; ?>
This Wordpress function needs any valid user ID. If you want to get the current user's ID to pass in, add this line first:
$user_id = wp_get_current_user()->ID;
Plan or download
Require a subscription to the Big Awesome plan or require the Super Rad download.
<?php if ( is_subscribed_to_memberful_plan( '154-big-awesome' ) || has_memberful_download( '27-super-rad' ) ) : ?>
Shown only to members with a subscription to the Big Awesome plan or the Super Rad download.
<?php endif; ?>
Check against a different member / WordPress user
These functions check against the current (signed in) member / WordPress user by default. To specify a different member / WordPress user, pass the function an optional WordPress user ID argument:
<?php if ( is_subscribed_to_memberful_plan( array( '27-super-rad', '59-rock-on' ), $user_id ) ) : ?>
Shown if $user_id is subscribed to the Super Rad or Rock On plans.
<?php endif; ?>
Check if a user has access to a post based on the Restrict Access tool
Configure access to your posts with the Restrict Access tool and use memberful_can_user_access_post()
to check if a user has access to a post or not:
<?php if ( memberful_can_user_access_post( get_current_user_id(), $post->ID) ) : ?>
Shown to members that have access based on the Restrict Access Tool.
<?php else : ?>
Shown to others.
<?php endif; ?>
Please note that you only need to do this if you want to show users some custom content (e.g. content from custom fields). If you only use the_content()
in your templates, then the Restrict Access tool will work without any template modifications.
Add a custom function that checks if a member is on a trial
Add the following custom function to your functions.php
file:
function is_on_trial() {
if ( ! function_exists( "memberful_wp_user_plans_subscribed_to" ) ) {
return false;
}
$subscriptions = memberful_wp_user_plans_subscribed_to(get_current_user_id());
if( empty( $subscriptions ) ) {
return false;
}
foreach( $subscriptions as $subscription ) {
if ( $subscription["in_trial_period"] ) {
return true;
}
}
return false;
}
Now is_on_trial()
will return true
if the member is on a trial (for any subscription).
Show profile information in WordPress themes
We've included a few functions for adding profile information to your theme template files:
memberful_account_url
memberful_sign_in_url
memberful_sign_out_url
Here's an example of using these functions to generate a profile box:
<?php if ( is_user_logged_in() ) : ?>
<div class="profile-box signed-in">
<a class="profile" href="<?php echo memberful_account_url(); ?>">
<?php echo get_avatar( wp_get_current_user()->user_email, 48 ); ?>
<?php echo wp_get_current_user()->display_name; ?>
</a>
<a class="sign-out" href="<?php echo memberful_sign_out_url(); ?>">Sign out</a>
</div>
<?php else : ?>
<div class="profile-box signed-out">
Already a customer? Please <a title="Sign in" href="<?php echo memberful_sign_in_url(); ?>">sign in</a>.
</div>
<?php endif; ?>
Link to downloads
You can use the memberful_account_get_download_url($slug)
function to link to a download.
The parameter to this function must be the slug of the download you're linking to. The slug for a download is shown at the bottom of the page when editing that download.
<a href="<?php echo memberful_account_get_download_url( '24-my-download' ); ?>">Download my ebook!</a>
Link to podcasts
If you're using the private podcasts feature inside of Memberful, you can use the memberful_wp_feed_url($id)
function to link to a member's unique, tokenized RSS feed.
The parameter to this function must be the ID of the podcast you're linking to. If the member doesn't have access to the given podcast, it returns null
.
<a href="<?php echo memberful_wp_feed_url( '24' ); ?>">Subscribe to my podcast!</a>
Link to private RSS feeds
If you've turned on private RSS feeds, you can share a link to the member's private RSS feed using this function:
<?php memberful_private_rss_feed_link( "Your RSS feed", "You don't have access." ); ?>
Related help docs:
- Restrict access to WordPress posts in bulk.
- Restrict access to entire categories or tags.
- Restrict parts of your WordPress post or page.