Restricting Content in Template Files
Table of Contents
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.
Please note: any changes you make to theme files should be done in a child theme, so that you don't lose your customizations when you install theme updates. If you don't understand this, please consider hiring a developer.
Restricting content to paid subscribers
The main function you will use is rcp_is_active(). It is a very simple function to use:
<?php if( rcp_is_active() ) : ?> <p>This content is restricted to active subscribers.</p> <?php endif; ?>
Restricting content to paid or free subscribers
That will limit the content between the conditional to members that have an active subscription. Note: members with a free subscription level are not covered by rcp_is_active(). For free users, you need a different check.
<?php if ( rcp_is_active() || ( rcp_get_subscription_id() && 'free' === rcp_get_status() ) ) : ?> <p>This content is restricted to active paid and active free subscribers.</p> <?php endif; ?>
Checking for a specific subscription level
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 subscribers that have a specific subscription level:
<?php if( rcp_is_active() && 2 == rcp_get_subscription_id() ) : ?> <p>This content is restricted to active subscribers with a subscription level ID of 2.</p> <?php endif; ?>
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>This content is only 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 the 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_is_active() ) : ?> <?php comment_form(); ?> <?php endif; ?>