How can you add a custom field to the table under View/Search Clients in the admin portal.
I have seen a lot of examples for ClientArea but nothing for the ADMIN Area.
Any light shed on this would be truly helpful.
Thanks.
How can you add a custom field to the table under View/Search Clients in the admin portal.
I have seen a lot of examples for ClientArea but nothing for the ADMIN Area.
Any light shed on this would be truly helpful.
Thanks.
You can do that with Javascript + AJAX:
1- write script to monitor current page is Clients page.
2- then add column header for the custom field,
3- Ajax script will send a request to php file (you need to write that as well) to get that custom field value and add it to the client row.
Best approach is to write addon module for this and use WHMCS hooks to add scripts to footer.
Use the AdminAreaFooterOutput hook to add javascript to the footer that adds the column.
The following is a working example that adds an arbitrary custom field to the table. Copy and paste the code into the hooks.php file of a custom Addon, then edit to meet your needs.
function add_custom_column_clients_list($vars)
{
// Only run this hook on the clients.php page
if ($vars['filename'] !== 'clients')
{
return;
}
// EDIT THESE!
$columnHeader = 'Your Custom Field';
$customFieldId = 123;
// Fetch all clients
$clients = localAPI('GetClients', ['limitnum' => 999]);
// Create an array to store client ids and their custom field values
$customFieldValues = [];
// Fetch the custom field value for each client
foreach ($clients['clients']['client'] as $client)
{
$customFieldValue = Capsule::table('tblcustomfieldsvalues')
->where('fieldid', $customFieldId)
->where('relid', $client['id'])
->value('value');
$customFieldValues[$client['id']] = $customFieldValue;
}
$customFieldValuesJSON = json_encode($customFieldValues);
$output = '<script>
document.addEventListener("DOMContentLoaded", function(event) {
var table = document.getElementById("sortabletbl0");
var headerRow = table.rows[0];
var headerCell = document.createElement("TH");
headerCell.innerHTML = "' . $columnHeader . '";
headerRow.appendChild(headerCell);
var customFieldValues = ' . $customFieldValuesJSON . ';
for (var i = 1, row; row = table.rows[i]; i++) {
var userId = row.cells[1].innerText;
var cell = row.insertCell(-1);
cell.innerHTML = customFieldValues[userId] ?? "";
}
});
</script>';
return $output;
}
add_hook('AdminAreaFooterOutput', 1, 'add_custom_column_clients_list');