# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: shortcode, members, visibility
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/logged-in-logged-out-content-shortcodes/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Content for Logged-In or Logged-Out Visitors" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
