Members-Only Content After the "More" Tag
Shows visitors everything above the "More" block of a post and keeps the rest for logged-in members, with a login prompt where the content stops.
Opens WPIDE on
Code
// Category slugs to apply this to; leave empty for all posts.
$wpide_more_categories = array();
add_filter( 'the_content', function ( $content ) use ( $wpide_more_categories ) {
if ( is_user_logged_in() || ! is_singular( 'post' ) || ! in_the_loop() ) {
return $content;
}
$raw = get_post_field( 'post_content', get_the_ID() );
if ( false === strpos( $raw, '<!--more' ) || ( $wpide_more_categories && ! has_category( $wpide_more_categories ) ) ) {
return $content;
}
$parts = get_extended( $raw );
$teaser = apply_filters( 'wpide_members_more_teaser', do_blocks( $parts['main'] ) );
$box = '<div class="wpide-members-only"><p>The rest of this post is for members. <a href="' . esc_url( wp_login_url( get_permalink() ) ) . '">Log in</a>';
if ( get_option( 'users_can_register' ) ) {
$box .= ' or <a href="' . esc_url( wp_registration_url() ) . '">join for free</a>';
}
return wp_kses_post( wpautop( $teaser ) ) . $box . ' to keep reading.</p></div>';
}, 1 );
Notes
Only posts that contain a More block (or <!--more-->) are affected. Limit it to some categories with $wpide_more_categories.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.