admin管理员组

文章数量:1126327

How do I send this line of code to the webhook? That is, when it gives the activate alarm, it should be sent to the webhook WordPress PHP:

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);
} );

How do I send this line of code to the webhook? That is, when it gives the activate alarm, it should be sent to the webhook WordPress PHP:

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);
} );
Share Improve this question edited Jan 19, 2024 at 7:24 bueltge 17.1k7 gold badges61 silver badges97 bronze badges asked Jan 13, 2024 at 10:16 RezaEllarosRezaEllaros 1 1
  • can you describe what you want without referring to a webhook? A webhook is just a name for a URL that gets visited/pinged when something happens, what you're asking for doesn't make sense – Tom J Nowell Commented Jan 13, 2024 at 14:37
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: Send Webhook WordPress PHP