Content for Logged-In or Logged-Out Visitors
Adds [members_only] and [guests_only] shortcodes so you can show different text, links or offers to visitors depending on whether they are logged in.
Opens WPIDE on
Code
add_shortcode( 'members_only', function ( $atts, $content = '' ) {
$atts = shortcode_atts( array( 'message' => '' ), $atts, 'members_only' );
if ( is_user_logged_in() ) {
return do_shortcode( (string) $content );
}
return '' !== $atts['message'] ? '<p class="wpide-members-only">' . esc_html( $atts['message'] ) . ' <a href="' . esc_url( wp_login_url( get_permalink() ) ) . '">Log in</a></p>' : '';
} );
add_shortcode( 'guests_only', function ( $atts, $content = '' ) {
return is_user_logged_in() ? '' : do_shortcode( (string) $content );
} );
Notes
[members_only message="Please log in to read this."] shows the message to guests instead of nothing.
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.