You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress中钩子自定义表数据插入事件并推送至REST API

Solution to Send Custom Table Data to REST API After Insert

To hook into the insert operation of your wp_jet_testing_data_table (or your prefixed table name) and send data to a REST API without modifying the original plugin, use the following approach in your custom plugin:

Step 1: Add This Code to Your Custom Plugin

<?php
/**
 * Plugin Name: Send Custom Table Data to API
 * Description: Automatically sends data from jet_testing_data_table inserts to a REST API.
 * Version: 1.0
 * Author: Your Name
 */

// Hook into WordPress database query execution
add_action('wpdb_query', 'send_testing_table_data_to_api', 10, 2);

function send_testing_table_data_to_api($query, $wpdb) {
    // Define your target table (matches the original code's table name)
    $target_table = $wpdb->prefix . 'jet_testing_data_table';
    
    // Check if the query is an INSERT into our target table
    if (strpos($query, "INSERT INTO `{$target_table}`") === 0) {
        // Get the ID of the newly inserted row
        $inserted_id = $wpdb->insert_id;
        
        // Fetch the full inserted data from the table
        $inserted_data = $wpdb->get_row(
            $wpdb->prepare("SELECT * FROM {$target_table} WHERE id = %d", $inserted_id),
            ARRAY_A
        );
        
        if ($inserted_data) {
            // Replace with your actual REST API endpoint
            $api_endpoint = 'https://your-api-url.com/endpoint';
            
            // Send data to the API
            $response = wp_remote_post($api_endpoint, [
                'headers' => ['Content-Type' => 'application/json'],
                'body' => json_encode($inserted_data),
                'timeout' => 10, // Adjust timeout as needed
            ]);
            
            // Optional: Log errors if API request fails
            if (is_wp_error($response)) {
                error_log('API Request Failed: ' . $response->get_error_message());
            }
        }
    }
}

Key Details:

  • Target Table Check: The code filters for INSERT queries targeting your specific custom table, so it won't interfere with other database operations.
  • Fetch Inserted Data: Using $wpdb->insert_id, we retrieve the exact row that was just inserted—this ensures we send the same data stored in the database, including auto-generated fields like id.
  • WordPress HTTP Request: wp_remote_post is used instead of raw curl to maintain compatibility with WordPress environment settings (like proxies or SSL configurations).
  • Error Logging: Optional error logging helps debug issues if the API request doesn't succeed.

Important Notes:

  • Replace https://your-api-url.com/endpoint with your actual REST API URL.
  • Ensure $target_table matches the table name used in the original code. If your table is actually named wp_testing_data_table (without the jet_ prefix), adjust this variable accordingly.

内容的提问来源于stack exchange,提问作者Diego

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 13:16:20