Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.