Created
October 10, 2024 11:42
-
-
Save hazratbilal0079/871db802d07b48fc164468e0fca6e84a to your computer and use it in GitHub Desktop.
Display and Hide Columns based on the user role using Data tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function to display the current user's role | |
function display_current_user_role_shortcode() { | |
// Check if a user is logged in | |
if ( is_user_logged_in() ) { | |
// Get the current user's data | |
$current_user = wp_get_current_user(); | |
// Retrieve the user's roles | |
$roles = $current_user->roles; | |
// Check if the user has any roles | |
if ( !empty( $roles ) ) { | |
// Optionally, you can format the roles for better readability | |
$formatted_roles = array_map( 'ucfirst', $roles ); | |
return implode( ', ', $formatted_roles ); | |
} else { | |
return 'No roles assigned.'; | |
} | |
} else { | |
return 'No user is currently logged in.'; | |
} | |
} | |
// Register the shortcode [user_role] | |
add_shortcode( 'user_role', 'display_current_user_role_shortcode' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Libraries: | |
<!-- DataTables CSS --> | |
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css"/> | |
<!-- jQuery --> | |
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> | |
<!-- DataTables JS --> | |
<script type="text/javascript" src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script> | |
Code: | |
<script> | |
jQuery(document).ready(function($) { | |
// Retrieve the user's role from the div with ID 'brxe-genant' | |
var userRole = $('#brxe-genant').text().trim().toLowerCase(); // Convert to lowercase for easier comparison | |
// Define a mapping of roles to the columns they can view | |
var roleColumnMap = { | |
'administrator': [], // Administrator: Can view all columns (no hidden columns) | |
'ticket_submitter': [5, 6, 7, 8], // Ticket_submitter: Hide Priority, Status, Category, and Assigned To | |
'ticket_handler': [7, 8] // Ticket_handler: Hide Category and Assigned To | |
}; | |
// Determine which columns should be hidden for the current user role | |
var columnsToHide = roleColumnMap[userRole] || []; // If the role is not in the map, default to viewing all columns | |
// Initialize the DataTable with the defined column visibility and disable ordering | |
var table = $('.jet-dynamic-table').DataTable({ | |
paging: false, // Disable pagination | |
searching: false, // Disable searching | |
info: false, // Disable table information | |
ordering: false, // Disable ordering (sorting) for all columns | |
columnDefs: columnsToHide.length > 0 ? [{ targets: columnsToHide, visible: false }] : [], // Apply hidden columns if necessary | |
responsive: true // Make the table responsive | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment