Okay
  Public Ticket #1377736
Hide/Show Columns Based on User Role
Closed

Comments

  •  1
    chaddly started the conversation

    Good morning! 

    First of all, datatables is TERRIFIC, just have to get that out of the way.  So good job to you and your team.

    Secondly, my problem.  Is there a way in the wpdatatables plugin to hide columns based on what type of user is logged in?  For instance, I have a few columns that contain sensitive data (personal phone number, personal address, etc.) that the general public should not see.  Can I make these columns appear to only those who are logged in as a certain user role, such as "Editor"?

    I know I can do this sort of thing with Javascript or php but I want to make sure that the wpdatatables plugin doesn't already have this feature built in.  Also, if a separate script is what I will end up needing, do you already have a sample script I can go by to save me some time.

    Thanks for all the help, you guys rock!

  •  472
    Isidora replied

    Hi chaddly,
    Thank you for your purchase.

    Thank you so much, we appreciate that.

    Sorry for disappointment, but something like this is not possible with built in features of plugin. When you are using option "Users see and edit only own data"  users will see only own data(but for hole row of table). It's not possible to hide columns based on login user.

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

    You can try our wpDataTables add-ons before purchase on these sandbox sites:

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables

  •  1
    Lethalmiko replied

    @chaddly,

    I also wanted this feature so my workaround is to simply create different tables with different columns for different users. I then use custom functions in functions.php to select which table is visible depending on which user group is viewing (calling the table using do_shortcode). Anonymous users can see the reduced table without those sensitive columns. I created a full table for logged in users and duplicated it into a new table with some columns removed.

  •  1
    chaddly replied

    Hey Lethalmiko, that sounds like what I need!  Any chance you can send me the function to to use as a template?

  •  1
    Lethalmiko replied

    @chaddly,

    Place this example code in your functions.php file in your currently activated theme. Edit as necessary to add or remove roles and put corresponding table numbers.

    ----------------------------------------------------------------------------

    function display_table_by_role_shortcode() {
        $current_user = wp_get_current_user();
        $user_role = $current_user->roles[0];
        
        if($user_role == 'administrator')
        {
        $table = "[wpdatatable id=1 table_view=regular]"; //table with all columns
        }
        elseif($user_role == 'Editor')
        {
        $table = "[wpdatatable id=2 table_view=regular]"; //table with fewer columns
        }
        else
        {
        $table = "[wpdatatable id=3 table_view=regular]"; //table for everyone else including anonymous users
        }

        echo do_shortcode($table);

    }

    add_shortcode('display_table_by_role', 'display_table_by_role_shortcode');

    ----------------------------------------------------------------------------

    You then put the shortcode in your page as follows:

    ----------------------------------------------------------------------------

    [display_table_by_role]