# Show Content to Specific Roles

> Adds a [show_to] shortcode that reveals the text inside it only to users with the roles you list, such as editors or customers.

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

## Code

```php
add_shortcode( 'show_to', function ( $atts, $content = '' ) {
    $atts = shortcode_atts( array( 'roles' => '' ), $atts, 'show_to' );

    if ( ! is_user_logged_in() ) {
        return '';
    }

    $allowed = array_filter( array_map( 'trim', explode( ',', strtolower( $atts['roles'] ) ) ) );
    $user    = wp_get_current_user();

    if ( ! array_intersect( $allowed, (array) $user->roles ) ) {
        return '';
    }

    return do_shortcode( (string) $content );
} );
```

## Notes

Example: [show_to roles="editor,administrator"]Internal note[/show_to]. Content stays hidden from everyone else, including guests.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Show Content to Specific Roles" 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/
