Custom post restriction messages in Restrict Content Pro

Set up custom page restriction messages in RCP

When a website visitor doesn’t have access to restricted content, Restrict Content Pro has two settings to control the message that is shown: one for content that requires a paid subscription, and one for content that requires a free subscription. This works great for people who want fine-grained control over the upsell messages they show to people without an active subscription, but sometimes they want a custom message for a specific page to further refine their upsell messaging. Let’s look at how that can be done.

Enabling custom page-level restriction messages

To set up a custom restriction message at the post/page level, you need to get your hands dirty with a little code. No worries, though, it’s pretty easy.

The best place to put this code is in a custom plugin. If you’re not familiar with how to create a custom plugin, you can install Pluginception, use it to create a new custom plugin, paste in the code below, and activate your new custom plugin.

Now let’s get to the code.

/**
* This plugin adds support for custom restriction messages per post.
* If a custom message exists, it is used in place of the ones defined
* in the RCP settings under Restrict > Settings > General.
*/
/**
* Displays the custom message metabox on the post edit screen.
*/
function jp_rcp_post_level_restriction_message_metabox() {
global $post;
$content = get_post_meta( $post->ID, 'rcp_post_level_restriction_message', true );
?>
<hr>
<div class="rcp-metabox-field">
<p><strong><?php _e( 'Override restricted content messages for this post.', 'rcp' ); ?></strong></p>
<p>
<label for="rcp_post_level_restriction_message">
<?php wp_editor( $content, 'rcp_post_level_restriction_message', array( 'drag_drop_upload' => true ) ); ?>
</label>
</p>
</div>
<?php
wp_nonce_field( 'rcp_post_level_restriction_message_nonce', 'rcp_post_level_restriction_message_nonce' );
}
add_action( 'rcp_metabox_fields_after', 'jp_rcp_post_level_restriction_message_metabox' );
/**
* Saves the custom message on the post edit screen.
*/
function jp_rcp_save_post_level_restriction_message( $post_id, $post ) {
if ( empty( $_POST['rcp_post_level_restriction_message_nonce'] ) || ! wp_verify_nonce( $_POST['rcp_post_level_restriction_message_nonce'], 'rcp_post_level_restriction_message_nonce' ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( empty( $_POST['rcp_post_level_restriction_message'] ) ) {
delete_post_meta( $post_id, 'rcp_post_level_restriction_message' );
return;
}
update_post_meta( $post_id, 'rcp_post_level_restriction_message', wp_kses_post( $_POST['rcp_post_level_restriction_message'] ) );
}
add_action( 'save_post', 'jp_rcp_save_post_level_restriction_message', 10, 2 );
/**
* Overrides the restricted message if a custom one exists for the current post.
*/
function jp_rcp_restricted_message_override( $message ) {
global $post;
$custom = get_post_meta( $post->ID, 'rcp_post_level_restriction_message', true );
if ( ! empty( $custom ) ) {
return $custom;
}
return $message;
}
add_filter( 'rcp_restricted_message', 'jp_rcp_restricted_message_override' );

How it works

The code above adds a new section to the Restrict this content box on the page edit screen. This is what it looks like.
Restrict Content Pro custom page restriction messages

That’s all there is to creating a custom restriction message for a page. If a visitor doesn’t have access to a piece of content, and you have a custom restriction message defined, the visitor will see that custom message instead of the global one defined in the RCP settings. Here’s a look at the example created in the screenshot above.
Restrict Content Pro custom page restriction message on the front end

And as always, if a page doesn’t have a custom restriction message defined, the global setting defined in Restrict Content Pro’s settings page under Restrict → Settings → General is used.

That’s all there is to it!

5 thoughts on “Custom post restriction messages in Restrict Content Pro

  1. triviumchina says:

    Doesn’t look like the metabox here runs shortcodes, and it would be awesome to run RCPs native shortcodes to be able to show different restriction messages depending on user’s memebership status. Any tips for adding shortcode functionality to the metabox? Thank youuu!

    • Hey, Alex. Sorry, I can’t say for sure. I haven’t worked on RCP or Woo in over a year now, and a lot has changed in both. And there’s the Gutenberg stuff, which in certain cases can affect metaboxes. Unfortunately I probably won’t have time to dig into that use case. If you find a solution, I’d love to hear it here in the comments.

Leave a Reply