Learn how to use WordPress functions to lock premium sections of your website. Reward paying members with exclusive access to your best content.
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 article, you’ll learn several functions that you can use in your WordPress themes or plugins to check member permissions, show profile information, or link to member benefits.
Most of these functions require you to provide the slug of a plan or download, or the ID of a podcast.To find the slug for a plan, go to Revenue → All plans and click the plan you want to find the slug for. The slug is shown in the summary section to the right.
The slug for a download can be found in the list of downloads under Content → Downloads.
You can also find it at the bottom of the page when editing a download.
Renaming a plan or a download will modify its slug, but the old slug will continue to work in WordPress functions. Feel free to rename your plans and downloads anytime, knowing that your existing functions will not break.
The ID for a podcast can be found in the URL while editing the podcast. It’s the numeric segment of this URL:
We’ve included three functions for checking if the current signed-in user is subscribed to plans, owns downloads, or has access to podcast feeds. Use them in your WordPress theme’s 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 podcast feeds, or is subscribed to at least one of the plans.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).
<?php if ( is_subscribed_to_memberful_plan( '154-big-awesome' ) ) : ?> Shown only to members with a subscription to the Big Awesome plan.<?php endif; ?>
Require a subscription to at least one of the listed plans:
Copy
<?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; ?>
Require a subscription to any plan.This function accepts any valid user ID, but you’ll typically want to use the current signed-in user, so we’ll get their user ID first.
Copy
<?php$user_id = wp_get_current_user()->ID;if ( is_subscribed_to_any_memberful_plan( $user_id ) ) : ?> Shown to members subscribed to **any** plan.<?php endif; ?>
Require a subscription to the Big Awesome plan or require the Super Rad download.
Copy
<?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; ?>
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:
Copy
<?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 metabox and use memberful_can_user_access_post() to check if a user has access to a post or not:
Copy
<?php$user_id = wp_get_current_user()->ID;if ( memberful_can_user_access_post( $user_id, $post->ID) ) : ?> Shown to members that have access based on the Restrict Access Tool.<?php else : ?> Shown to others.<?php endif; ?>
You only need to do this if you want to show users some custom content (e.g. content from custom fields).If you just need to protect content in the default WordPress content area (the_content() in your theme), the Restrict Access tool will be sufficient and you won’t need to use this function.
Add a custom function that checks if a member is on a trial
We can leverage the memberful_wp_user_plans_subscribed_to function to create a custom function that checks if a member is currently on a trial for any of their subscriptions.Add the following custom function to your functions.php file:
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.
Copy
<a href="<?php echo memberful_account_get_download_url( '24-my-download' ); ?>">Download my ebook!</a>
If you’re using the private podcasts feature inside Memberful, you can use the memberful_wp_feed_url($id) function to link to a member’s unique 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, so we should check for that before rendering the link.
Copy
<?php $url = memberful_wp_feed_url('24'); if ($url) { echo '<a href="' . esc_url($url) . '">Subscribe to the podcast!</a>'; } ?>