Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom post / page restriction messages in Restrict Content Pro
/**
* 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' );
@kendraschaefer

This comment has been minimized.

Copy link

@kendraschaefer kendraschaefer commented Feb 27, 2018

Dude, this is fabulous. Thank you.

@kendraschaefer

This comment has been minimized.

Copy link

@kendraschaefer kendraschaefer commented Mar 16, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.