整合指南 · WORDPRESS 和 CMS
無需 SMTP 外掛程式的 WordPress 聯絡表單集成
透過將 WordPress 聯絡人提交直接路由到 Lugatu 表單端點,消除失敗的 wp_mail() 傳送、wp_options 中的明文密碼儲存以及龐大的外掛程式。
1. WordPress SMTP 外掛的問題
大多數 WordPress 網站依賴 WP Mail SMTP 等外掛程式透過 PHP mail() 或儲存的 SMTP 憑證發送聯絡人通知。 當託管 IP 位址發生變更或託管提供者封鎖連接埠 587/465 時,查詢就會悄悄遺失。
2. 透過 wp_remote_post() 進行伺服器端集成
對於主題開發人員和自訂插件,可以使用工作區 API 金鑰從 WordPress 伺服器安全地轉發表單提交,而不會向前端訪客洩露秘密。
functions.php
php
<?php
// Secure WordPress server-side form forwarding
function handle_custom_contact_submission() {
if ( ! isset( $_POST['contact_nonce'] ) || ! wp_verify_nonce( $_POST['contact_nonce'], 'contact_action' ) ) {
wp_send_json_error( array( 'message' => 'Security check failed.' ), 403 );
}
// Silently drop automated spam if honeypot trap is filled
if ( ! empty( $_POST['_gotcha'] ) ) {
wp_send_json_success( array( 'message' => 'Thank you! Message received.' ) );
return;
}
$name = sanitize_text_field( $_POST['name'] ?? '' );
$email = sanitize_email( $_POST['email'] ?? '' );
$message = sanitize_textarea_field( $_POST['message'] ?? '' );
$payload = array(
'fields' => array(
'name' => $name,
'email' => $email,
'message' => $message,
),
);
$api_url = 'https://www.lugatu.com/api/v1/forms/' . LUGATU_FORM_GUID . '/submissions';
$response = wp_remote_post( $api_url, array(
'headers' => array(
'Authorization' => 'Bearer ' . LUGATU_API_KEY,
'Content-Type' => 'application/json',
'Idempotency-Key' => wp_generate_uuid4(),
),
'body' => wp_json_encode( $payload ),
'timeout' => 10,
) );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) >= 400 ) {
wp_send_json_error( array( 'message' => 'Failed to deliver message.' ), 500 );
}
wp_send_json_success( array( 'message' => 'Thank you! Message received.' ) );
}
add_action( 'wp_ajax_custom_contact', 'handle_custom_contact_submission' );
add_action( 'wp_ajax_nopriv_custom_contact', 'handle_custom_contact_submission' );
3. 直接沙盒嵌入 Iframe 選項
如果您喜歡零 PHP 程式碼,請使用我們的沙盒響應式 iframe 將已發布的 Lugatu 表單直接嵌入到任何 WordPress 頁面或 Elementor HTML 區塊上。
wordpress-embed.html
html
<iframe
src="https://www.lugatu.com/embed/forms/YOUR_PUBLIC_FORM_ID"
width="100%"
height="540"
frameborder="0"
style="border:none; border-radius:16px; overflow:hidden;"
title="Contact Form">
</iframe>
安全性和身份驗證不變
安全說明:在 Web 根目錄之外的 wp-config.php 中定義 LUGATU_API_KEY,切勿在客戶端可存取的腳本或公共主題檔案中定義。
故障排除和常見攝取錯誤
wp_remote_post() 回傳 cURL 錯誤 28:連線逾時
確保您的託管提供者允許連接埠 443 上到 www.lugatu.com 的出站 HTTPS 流量。
常見問題解答
我需要安裝特殊外掛才能在 WordPress 上使用 Lugatu 嗎?
不需要。您可以使用我們的輕量級官方 WordPress 連接器,也可以簡單地使用標準 HTML iframe 或 AJAX 片段嵌入表單。
準備好建立您的表單端點了嗎?
幾秒鐘內免費開始使用。 無需信用卡。