• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2764

Original Genesis Tutorials & 6000+ Guaranteed Code

Snippets

  • Premium Access
  • Log in

Force Logged Out Users To Register To View Full Content

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:

register

You’ll need to enable the registration form in the general settings.

enable-registration

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;
}

Related Code Snippets

  • Hide Posts From Logged Out Users
  • Limit Content For Logged Out Users

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags
 

Loading Comments...
 

You must be logged in to post a comment.