• Resolved Vladimirrider

    (@vladimirrider)


    Tell me please, how to specify that users of two roles should see this element?
    For example, if an element is to be seen by users with the Editor role and users with the Author role.

Viewing 2 replies - 1 through 2 (of 2 total)
  • moongear

    (@moongear)

    Add a function to your child theme functions.php file. Edit the roles to suit your needs. It might look like this:

    function user_role_can_see_this() {
        // Which Roles
        $which_roles = array(
            'author'.
            'editor'
        );
        $user = wp_get_current_user();
        return in_array( $which_roles, (array) $user->roles );
    }

    Once that is uploaded, add the function user_role_can_see_this() to the Visibility part of the section in DIVI that you want to be visible only to your chosen roles.

    That should do it!

    • This reply was modified 6 years ago by moongear.

    This isn’t working for me.

    I changed the variable to $userCurrent instead of $user (I have some other API coding that uses $user.

    I have this:

    function user_role_can_see_this() {
        // Which Roles
        $which_roles = array(
            'administrator'.
            'associate_member'.
    		'full_member'
        );
        $userCurrent = wp_get_current_user();
        return in_array( $which_roles, (array) $userCurrent->roles );
    }

    and then on the section (it’s a section and a module within) that I want to display I have this:
    user_role_can_see_this()

    It’s not showing up when I’m logged in with an administrator level account.

    I tried logging in with a user that has both of the other roles, and it hasn’t worked on those either.

    I have the latest version of WP and Divi installed.

    • This reply was modified 5 years, 6 months ago by Wow Digital.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to use for two roles or more’ is closed to new replies.