WHMCS域名监控配置模块默认添加自定义字段方法咨询
Absolutely—you can get those custom fields to show up by default on the WHMCS order page for your domain monitoring module. Let’s break down the most straightforward, reliable ways to do this, tailored to your workflow where you send API requests to an external monitoring server in the _CreateAccount function:
1. Use WHMCS Product Custom Fields (Quickest Setup)
If you want these fields tied directly to your monitoring service plan, the easiest way is to configure them via WHMCS’s built-in product settings:
- Head to Setup > Products/Services > Products/Services and edit your target monitoring product.
- Switch to the Custom Fields tab, then click Add New Custom Field.
- For your domain input:
- Set the type to
Text Box, label it something like Domain to Monitor, check the Required box, and make sure "Show on Order Form" is enabled under Display Options.
- Set the type to
- For your custom dropdown:
- Set the type to
Dropdown, add your predefined options, mark it as required if needed, and again enable "Show on Order Form".
- Set the type to
- Save the fields, and they’ll automatically appear whenever a user selects your monitoring product during checkout.
2. Enforce Dynamic Field Visibility with Module Hooks (For Advanced Control)
If you need more flexibility—like conditional field display or tying fields directly to your module’s logic—use WHMCS hooks in your module:
- Create a
hooks.phpfile in your module’s directory (if you don’t already have one). - Use the
ClientAreaPageCheckouthook to inject your fields only when the user selects your monitoring product:
add_hook('ClientAreaPageCheckout', 1, function($vars) { // Replace these with your actual product IDs $targetProductIds = [1, 3]; $selectedProduct = $vars['cart']['products'][0]['id'] ?? null; if (in_array($selectedProduct, $targetProductIds)) { // Add domain input field $vars['customfields'][] = [ 'id' => 'monitoring_domain', 'name' => 'Domain to Monitor', 'type' => 'text', 'required' => true, 'value' => '', 'showonorder' => true ]; // Add custom dropdown field $vars['customfields'][] = [ 'id' => 'monitoring_preference', 'name' => 'Monitoring Level', 'type' => 'dropdown', 'required' => true, 'options' => [ 'basic' => 'Basic Uptime Monitoring', 'premium' => 'Premium Uptime + SSL Checks' ], 'showonorder' => true ]; } return $vars; });
This ensures the fields only appear for your specific product, and you can tweak their behavior right from your module code.
3. Access Field Values in _CreateAccount to Send Your API Request
Once the user submits the order, you can pull the custom field values in your module’s _CreateAccount method to send the request to your monitoring server:
function yourmodule_CreateAccount($params) { // Grab the custom field values from the order params $domain = $params['customfields']['monitoring_domain'] ?? ''; $selectedPreference = $params['customfields']['monitoring_preference'] ?? ''; // Validate the domain first (add your own checks here) if (empty($domain) || !filter_var('https://' . $domain, FILTER_VALIDATE_URL)) { return [ 'status' => 'error', 'description' => 'Please enter a valid domain name to monitor.' ]; } // Send API request to your external monitoring server $apiEndpoint = 'https://your-monitor-server.com/api/add-monitor'; $postData = [ 'domain' => $domain, 'monitor_level' => $selectedPreference, 'client_id' => $params['clientid'] // Pass WHMCS client ID for tracking ]; // Use cURL to send the request (you can use Guzzle too if preferred) $ch = curl_init($apiEndpoint); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $apiResponse = curl_exec($ch); curl_close($ch); // Handle the API response $responseData = json_decode($apiResponse, true); if ($responseData['success'] ?? false) { return [ 'status' => 'success', 'description' => 'Domain monitoring has been successfully activated.' ]; } else { return [ 'status' => 'error', 'description' => 'Failed to set up monitoring: ' . ($responseData['message'] ?? 'Unknown error occurred.') ]; } }
Quick Pro Tips
- Always mark critical fields as Required to prevent incomplete orders.
- Add client-side validation (via JavaScript in your module’s client area files) to give users immediate feedback if they enter an invalid domain.
- Test the full workflow end-to-end: Place a test order, confirm the fields appear, check that the API request is sent correctly, and verify WHMCS receives the success/error status from your module.
内容的提问来源于stack exchange,提问作者Pejman

