• Hi,
    is there a way to hide most of the fields in the user profile but only for profiles belonging to a specific role (custom UM role)? What I mean is that the admin should see all the fields on the default admin role profile (an any other default roles), but the custom role should be stripped of almost all default fields leaving only some custom ones coming from UM and ACF. So not depending on the role of the logged in user, but the role of the user whoes profile is being viewed and edited.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @osiak

    Unfortunately, this requires customization on your end.

    You can try this filter hook to modify the condition for um_can_view_field:

    add_filter("um_can_view_field","um_custom_can_view_field", 10, 2);
    function um_custom_can_view_field( $can_view, $data ){
    
       return $can_view;
    }

    For um_can_edit_field:

    add_filter("um_can_edit_field","um_custom_can_edit_field", 10, 2);
    function um_custom_can_view_field( $can_edit, $data ){
    
       return $can_edit;
    }

    Regards,

    Thread Starter Chris

    (@osiak)

    Not sure how to use this to target a specific role being viewed. Could you please elaborate a little on that. Thx.

    • This reply was modified 4 years, 11 months ago by Chris.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @osiak

    You can use this function to retrieve the current user profile ID:
    $user_id = um_profile_id()

    With the user ID, you can fetch the user data with um_fetch_user( $user_id )

    Once the current viewing profile is set, you can now retrieve the role with um_user("role").

    So you could try this code snippet:

    // View mode

    <?php 
    add_filter("um_can_view_field","um_custom_can_view_field", 10, 2);
    function um_custom_can_view_field( $can_view, $data ){
    
       if( um_is_core_page("user") ){ // only runs the code for profile page 
            um_fetch_user( um_profile_id() );
            if( "subscriber" == um_user("role") ){ // show view field for subscriber
                 return true;
            }  
       }
     
       return false; // hide all view fields
    }
    ?>

    // Edit Mode

    <?php
    add_filter("um_can_edit_field","um_custom_can_edit_field", 10, 2);
    function um_custom_can_edit_field( $can_view, $data ){
    
       if( um_is_core_page("user") ){ // only runs the code for profile page 
            um_fetch_user( um_profile_id() );
            if( "subscriber" == um_user("role") ){ // show edit field for subscriber
                 return true;
            }  
       }
     }
    ?>

    Regards,

    • This reply was modified 4 years, 11 months ago by Champ Camba.
    • This reply was modified 4 years, 11 months ago by Champ Camba.
    Thread Starter Chris

    (@osiak)

    I did the above for my custom UM user role and it didn’t hide any fields on the profile page of this user when viewing as admin…

    Thread Starter Chris

    (@osiak)

    Here is what I tried (to hide the color scheme), but it didn’t work:

    add_filter("um_can_view_field","um_custom_can_view_field", 10, 2);
    function um_custom_can_view_field( $can_view, $data ){
    
       if( um_is_core_page("user") ){ 
            um_fetch_user( um_profile_id() );
            if( "um_my_custom_role" == um_user("role") ){
                 um_is_core_page();
    			echo '<style> tr.user-admin-color-wrap { display: none; } </style>';
            }  
       }
    }
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @osiak

    What’s the meta key of the color scheme field?

    Please try this code snippet and replace the meta key {insert meta key} with the meta key of the color scheme field.

    add_filter("um_can_view_field","um_custom_can_view_field", 10, 2);
    function um_custom_can_view_field( $can_view, $data ){
    
       if( um_is_core_page("user") ){ 
            um_fetch_user( um_profile_id() );
            if( "um_my_custom_role" == um_user("role") && "{insert meta key}" == $data['metakey'] ){
                
    		return false;
            }  
       }
        return true;
    }
    Thread Starter Chris

    (@osiak)

    Hi, the user profile sections have a class and based on that action can be taken. Eg. the color scheme field has a class of “user-admin-color-wrap”, but I tried this and it doesn’t work:

    add_filter("um_can_view_field","um_custom_can_view_field", 10, 2);
    function um_custom_can_view_field( $can_view, $data ){
    
       if( um_is_core_page("user") ){ 
            um_fetch_user( um_profile_id() );
            if( "um_my_custom_role" == um_user("role") && "user-admin-color-wrap" == $data['metakey'] ){
                
    		return false;
            }  
       }
        return true;
    }

    I also tried replacing $data['metakey'] with $data['class'] but it doesn’t work either.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Hide user profile fields for specific UM roles’ is closed to new replies.