Members-only content shortcode
Wrap text in [members_only]…[/members_only] and only logged-in users see it. Visitors see a short message with a login link instead.
<?php
// [members_only]Only logged-in users see this.[/members_only]
if ( is_user_logged_in() ) {
return do_shortcode( (string) $content );
}
$here = is_singular() ? get_permalink() : home_url( '/' );
return '<p class="members-only">This part is for members. <a href="' . esc_url( wp_login_url( $here ) ) . '">' . esc_html__( 'Log In' ) . '</a></p>';
Good to know
Hides content on the page, not in the database or feeds with full text: don’t use it for anything truly secret. Page caches must skip logged-in users (most do).
How to use it
Type [members_only] in a post, a page or a Shortcode block.
In these packs
- Shortcodes: Handy shortcodes for dates, breadcrumbs, buttons, share links, members-only content and more.