Extending User Management
Developer Tutorial: Extending User Management
Learn how to customize the user editing process using the admin_useredit_snippet.php.
1. How it Works
The system automatically looks for a file named admin_useredit_snippet.php in your active setup folder (e.g., setup_myapp/). If found, it is included in admin_useredit.php at two specific points:
Executed after the user data has been successfully updated in the database.
Condition: if (isset($updated_rows) && $updated_rows > 0)
Use this to send emails, call APIs, or update related data.
Executed inside the HTML form, just before the submit buttons.
Condition: Default execution flow.
Use this to add checkboxes, inputs, or information text to the form.
2. Real-World Example: Sending Notification Emails
A common requirement is to send specific emails to users when their status changes (e.g., "Welcome to the Full Version" or "Account Archived"). Here is how we implemented this.
Step A: The UI (Adding Checkboxes)
We add switches to the form so the administrator can decide whether to send an email.
// --- PART 2: UI (Displayed in the form) ---
?>
<hr class="my-4">
<h4 class="h6 text-muted mb-3">Notifications & Actions</h4>
<div class="form-check form-switch mb-2">
<input class="form-check-input" type="checkbox" role="switch" id="send_contract_mail" name="send_contract_mail" value="1">
<label class="form-check-label" for="send_contract_mail">
<i class="fa-solid fa-envelope me-2"></i>Send Welcome Mail (Contract)
</label>
<div class="form-text">Sends login data and shop link to the user.</div>
</div>
Step B: The Logic (Sending the Mail)
We check if the update was successful and if the checkbox was checked. Then we use PHPMailer to send a formatted HTML email.
<?php
// --- PART 1: LOGIC (Executed only after successful DB update) ---
if (isset($updated_rows) && $updated_rows > 0) {
// 1. Check if the checkbox was selected
if (!empty($_POST['send_contract_mail'])) {
// 2. Prepare content
$app_name = defined('SETUP_NAME') ? SETUP_NAME : 'My App';
$subject = "Your License for " . $app_name;
$body = "Hello <strong>" . htmlspecialchars($username) . "</strong>,\n\n";
$body .= "Your account has been upgraded to 'Contract'.\n";
// ... add more content ...
// 3. Send Mail (using a helper function or PHPMailer directly)
// Note: $email and $username are available from the main script scope
$send_html_mail($email, $subject, nl2br($body), strip_tags($body));
}
// Important: Return to prevent the UI part from rendering during the redirect phase
return;
}
?>
3. More Ideas & Possibilities
The admin_useredit_snippet.php is a powerful tool. Here are some other things you can do with it:
CRM Synchronization
Automatically push user data to an external CRM (like HubSpot or Salesforce) via REST API when a user is updated or upgraded.
Initialize User Data
When a user is switched from "Demo" to "Contract", automatically create a set of standard records (e.g., a "Welcome Project") in their team's tables.
Billing Trigger
Trigger a webhook to your accounting software to generate an invoice when the license type changes.