This code enables you to restrict the content on single posts and pages to any character limit for logged out users. The code then displays a message with link to the registration form:
You’ll need to enable the registration form in the general settings.
The code works in any theme.
Here’s the code & sample CSS for logged in members:
Add the following PHP code to your child themes functions file:
add_filter( 'the_content', 'registration_link', 10, 1 );
function registration_link( $content ) {
$limit = wp_trim_words( get_the_content(), 40, '.....' );
$register = sprintf( '<p class="note"><a href="%s">' . __( 'You must be logged in to view content. Register here.' ) . '</a></p>', esc_url( wp_registration_url() ) );
$limit = $limit . $register;
$output = ! is_user_logged_in() && is_singular(array( 'post','page' ) ) ? $limit : $content;
return $output;
}
Sample CSS
.note {
background-color: #F5F5F5;
border: 1px solid #DDDDDD;
padding: 20px;
text-align: center;
margin-top: 20px;
margin-bottom: 20px;
}
Leave a Reply