Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

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.

  • Type PHP
  • Runs Where you put the [members_only] shortcode
  • Category Shortcodes
  • Tested with WordPress 7.1
PHP
<?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.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.