1
votes

I'm using a wordpress plugin (WpDataTables), and want to change the table shown to users based on their user role.

I added the script below to my functions.php, but it's only working for my admin user and is not recognizing my custom roles, they are being sent to the "else" table (ID=4).

I've verified my custom roles exist and my other functions.php scripts like login redirect based on user roles work fine.

Any help would be great.

//Function to change table shown based on user role
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=5 table_view=regular]";
    }
    elseif($user_role = 'member1')
    {
    $table = "[wpdatatable id=1 table_view=regular]";
    }
    elseif($user_role = 'member2')
    {
    $table = "[wpdatatable id=2 table_view=regular]";
    }
    elseif($user_role = 'member3')
    {
    $table = "[wpdatatable id=3 table_view=regular]";
    }
    else
    {
    $table = "[wpdatatable id=4 table_view=regular]";
    }
    echo do_shortcode($table);

}

add_shortcode('display_table_by_role', 'display_table_by_role_shortcode');

Thanks

Brad

1

1 Answers

2
votes

we need to use == for comparing.

like

   if($user_role == 'administrator')
    {
    $table = "[wpdatatable id=5 table_view=regular]";
    }
    elseif($user_role == 'member1')
    { ```