add_action('template_redirect', function () { if (is_admin()) return; $flowise_url = get_option('flowiseaiapiurl', ''); $n8n_url = get_option('n8napiurl', ''); $input_value = $_POST['query'] ?? $_GET['query'] ?? null; if (!$input_value) return; $body = []; foreach ($_REQUEST as $key => $value) { $body[$key] = sanitize_text_field($value); } $args = [ 'body' => $body, 'timeout' => 30, ]; if (!empty($flowise_url)) { call_flowise_api($flowise_url, $args); } elseif (!empty($n8n_url)) { call_n8n_api($n8n_url, $args); } }); function call_flowise_api($url, $args) { $response = wp_remote_post($url, $args); handle_api_response($response); } function call_n8n_api($url, $args) { $response = wp_remote_post($url, $args); handle_api_response($response); } function handle_api_response($response) { header('Content-Type: text/plain; charset=utf-8'); if (is_wp_error($response)) { echo 'Error: ' . esc_html($response->get_error_message()); } else { echo wp_remote_retrieve_body($response); } exit; }