Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Last active February 23, 2018 07:41

Revisions

  1. maheshwaghmare revised this gist Feb 23, 2018. 1 changed file with 34 additions and 1 deletion.
    35 changes: 34 additions & 1 deletion check-current-user-role.php
    Original file line number Diff line number Diff line change
    @@ -1 +1,34 @@
    prefix_user_has_role
    <?php
    if( ! function_exists( 'prefix_user_has_role' ) ) :

    /**
    * Check logged in user has role
    *
    * @todo Change `prefix` with your own unique slug.
    *
    * E.g. var_dump( prefix_user_has_role( 'administrator' ) );
    *
    * Output:
    *
    * bool(true)
    *
    * @since 1.0.0
    * @param string $role User role.
    * @return boolean User role status.
    */
    function prefix_user_has_role( $role = '' )
    {
    // Not logged in? Return false.
    if( ! is_user_logged_in() ) {
    return false;
    }

    // Role exist? Return true.
    if ( in_array( $role, (array) wp_get_current_user()->roles ) ) {
    return true;
    }

    // Default. Return false.
    return false;
    }
    endif;
  2. maheshwaghmare created this gist Feb 23, 2018.
    1 change: 1 addition & 0 deletions check-current-user-role.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    prefix_user_has_role