集成指南 · 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 片段嵌入表单。
准备好创建您的表单端点了吗?
几秒钟内免费开始使用。 无需信用卡。