Shortcode for content view access restriction

Shortcode to display the content only to the specific roles

you can use this shortcode to show content only to specific user roles.

function cww_check_user_role( $atts, $content = null ) {
        extract( shortcode_atts( array(
                'role' => '' ), $atts ) );

        $user = wp_get_current_user();
        $allowed_roles = array_map('trim', explode(',', $role));
        if( array_intersect($allowed_roles, $user->roles ) ) {
                return $content;
add_shortcode( 'user_role', 'cww_check_user_role' );

How to use user role shortcode

You can see example below.

[user_role role=”aministrator”]Admin[/user_role]

[user_role role=”subscriber”]Subscriber[/user_role]

Leave a Comment

Your email address will not be published. Required fields are marked *