Restricting Content in Template Files

If you would like to restrict portions of your theme or plugin template files, Restrict Content Pro provides a series of functions that will allow you to do that. Consult the Function Reference for a complete list of the functions you may use.

Note: Any changes you make to Theme files should be done in a  child theme, so you don't lose your customizations when you install theme updates. If you don't understand this, please consider hiring a developer.

As of version 3.0.5, the main function you will use is  rcp_user_has_active_membership(). If you’re on an older version of Restrict Content Pro then you can use rcp_is_active() instead, in the exact same way.

They are very simple functions to use:

<?php if( rcp_user_has_active_membership() ) : ?>
	<p>Content inside here would only be visible to active members.</p>
<?php endif; ?>

or in versions prior to 3.0.5:

<?php if( rcp_is_active() ) : ?>
	<p>Content inside here would only be visible to active members.</p>
<?php endif; ?>

That will limit the content between the conditional to members that have an active subscription. If you want to limit the visibility only to customers who have a paid subscription you will need to use  rcp_user_has_paid_membership() instead.

<?php if ( rcp_user_has_paid_membership() ) : ?>
	<p>Content inside here would only be visible to active paid and active free subscribers.</p>
<?php endif; ?>

There are more advanced checks you can do (see the function reference linked above) as well. For example, you could show content to just active members that have a specific membership level.

If you’re on version 3.0.5 or higher you can use this:

<?php if( in_array( 2, rcp_get_customer_membership_level_ids() ) ) : ?>
	<p>Content inside here would only be visible to active members with a membership level ID of 2.</p>
<?php endif; ?>

Otherwise, if you’re on an older version of RCP you can use this:

<?php if( rcp_is_active() && 2 == rcp_get_subscription_id() ) : ?>
	<p>Content inside here would only be visible to active subscribers with a subscription level ID of 2.</p>
<?php endif; ?>

In both examples just change the number “2” to the ID number you want to check.

Restricting content based on metabox settings

If you’re adding restrictions to a custom page template, you may want to set up PHP restrictions based on your metabox settings for that page. That can be done using the  rcp_user_can_access() function. This checks to see if the current user has access to view the current page, based on the settings you’ve chosen in the “Restrict this content” meta box.

<?php if( rcp_user_can_access() ) : ?>
	<p>Content inside here would only be visible to people who meet the requirements you've set in the "Restrict this content" meta box.</p>
<?php endif; ?>

The RCP_Member class can be helpful for determining if a member has access to content as well.

Restricting access to posting comments

If you want to allow only active members to post comments on your site, you’ll need to customize your theme’s comments.php file.

Note: The comments.php file varies from theme to theme, but they generally have a similar structure.

To restrict access to the posting of comments, find this line in your comments.php file:

<?php comment_form(); ?>

Now let’s adjust it so that only active members can post comments.

<?php if ( rcp_user_has_active_membership() ) : ?>
	<?php comment_form(); ?>
<?php endif; ?>

Have more questions?

Submit a request