To send a line of code to a webhook when a WordPress plugin is activated, you’ll need to modify the register_activation_hook
code to include a function that sends a request to your desired webhook URL. Here’s how you can do it:
First, you need to set up a webhook URL where you want to send the notification.
Modify the activation hook function to send a request to this webhook.
Here’s an example of how you might modify your existing code:
register_activation_hook(__FILE__, function() {
$logFilePath = plugin_dir_path(__FILE__) . 'log.txt';
file_put_contents($logFilePath, date('Y-m-d H:i:s') . ': plugin Activate !' . PHP_EOL, FILE_APPEND);
// Webhook URL
$webhookUrl="http://your-webhook-url.com";
// Data you want to send
$data = ['message' => 'Plugin Activated', 'timestamp' => date('Y-m-d H:i:s')];
// Use cURL to send data to the webhook
$ch = curl_init($webhookUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Optional: Log the response from the webhook
file_put_contents($logFilePath, "Webhook response: " . $response . PHP_EOL, FILE_APPEND);
});
Replace http://your-webhook-url.com with the actual URL of your webhook.
The $data array contains the information you want to send. You can customize this as needed.
The curl functions are used to send a POST request to the webhook with the data.
Make sure that your server has cURL installed and enabled for this to work. Also, it’s important to test this in a safe environment first to ensure it doesn’t cause any unexpected issues with your WordPress installation.