How to transform WordPress user role code to WP shortcode?

You can add a parameter to your shortcode function and use this value to check if the user is allowed to view the content:

function func_check_user_role( $atts, $content = null ) {
    $user = wp_get_current_user();
    $user_role = $atts['role'];
    $allowed_roles = [];
    array_push($allowed_roles , $user_role);

if ( is_user_logged_in() && array_intersect($allowed_roles, $user->roles ) ) {
      return $content;
 } else {
    return '';
}

}
add_shortcode( 'check-user-role', 'func_check_user_role' );

And your shortcode looks like:

[check-user-role role="subscriber"] Your content [/check-user-role]

So the $atts are your attributes, the $content is your post content.

Check out the documentation:

https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/

You save the value inside of your $user_role variable and check, if your user is logged in and has the role which is inside of the user object (you got that in your $user). If this is true you return the content. If not true, there is nothing to be returned or maybe a string like “you are not allowed to view this content”.

Hope this helps!