【发布时间】:2022-11-17 04:49:37
【问题描述】:
在客户的主题中有一个自定义联系表单,不是插件,也不是流行的 ajax 表单 sn-p。我一开始看到这样的表格,想弄明白。它的动作是一个文件,它也在主题中。 表格代码
<form action="<?php echo get_template_directory_uri(); ?>/data.php" method="post" id="form2">
<ul class="clearfix">
<li><input type="text" name="nameUser" class="required" placeholder="Your name"></li>
<li><input type="tel" name="phoneUser" class="required phone" placeholder="Your phone"></li>
</ul>
<div class="btn-container clearfix">
<div class="wrap-btn-right">
<input type="hidden" name="letter_act" value="<?php echo get_field('letter_action', 'option'); ?>">
<button class="btn-gen" type="submit">Submit</button>
</div>
</div>
</form>
data.php 文件的代码,其中发送表单中的所有数据
<!DOCTYPE html>
<html>
<head>
<title>New request</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html {width: 100%;height: 100%;}
</style>
</head>
<body>
<?php
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
$to = "$_POST[letter_act]";
$from = "noreply@".$_SERVER['SERVER_NAME'];
$subject = '=?utf-8?B?'.base64_encode('New request:').'?=';
$headers .= 'Return-path: <' . $from . ">\r\n";
$headers .= 'From: request <' . $from . ">\r\n";
$headers .= 'Content-type: text/plain; charset=utf-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$message = "Name: $_POST[nameUser]\n\nPhone: $_POST[phoneUser]";
$mail = mail($to, $subject, $message, $headers);
if ($mail) {
echo "<body style='background: url(wp-content/themes/pzk-theme/img/bg-general.jpg) no-repeat center; background-size:cover;'><div style='width:260px;height:275px;margin:100px auto;background: #fff;color:#0F5F6A;text-align:center;padding:40px 30px 0;font-size: 130%;font-family: Arial,sans-serif;box-shadow:0 10px 15px rgba(0,0,0, .5);'><p>The form was successfully sent. Thank you! </p><p><a href='/' style='display:block;padding:10px 25px;margin: 10px 0 0;font-size:80%;background:#ff530e;border-radius:90px;border-bottom: 5px solid #d1450a;color:#fff;text-decoration:none;width:100px;margin:40px auto 0;'>To the home page</a></p></div>";
}
else {
echo "<body style='background: #fff;'><div style='width:260px;height:275px;margin:100px auto;background: #fff;color:#282828;text-align:center;padding:40px 30px 0;border: 1px solid #c0c0c0;border-radius:10px;font-size: 130%;font-family: Arial,sans-serif;box-shadow:0 10px 15px rgba(0,0,0, .5);'><p>Error! Please repeat your attempt later .</p><p><a href='index.html' style='display:block;padding:10px 25px;background:#00b5a0;border-radius:5px;color:#fff;text-decoration:none;text-shadow:1px 1px 1px #000;width:100px;margin:40px auto 0;'>To the home page</a></p></div>";
}
?>
</body>
</html>
我搜索并找到了方法 - 将这段代码粘贴到 data.php 文件的顶部
<?php header( "refresh:5;url='https://example.com/thank-you/'" );?>
它有效,重定向到所需的 url,但首先在发送后显示来自
if ($mail) {
echo "<body style='background: url(wp-content/themes/pzk-theme/img/bg-general.jpg) no-repeat center; background-size:cover;'><div style='width:260px;height:275px;margin:100px auto;background: #fff;color:#0F5F6A;text-align:center;padding:40px 30px 0;font-size: 130%;font-family: Arial,sans-serif;box-shadow:0 10px 15px rgba(0,0,0, .5);'><p>The form was successfully sent. Thank you! </p><p><a href='/' style='display:block;padding:10px 25px;margin: 10px 0 0;font-size:80%;background:#ff530e;border-radius:90px;border-bottom: 5px solid #d1450a;color:#fff;text-decoration:none;width:100px;margin:40px auto 0;'>To the home page</a></p></div>";
}
如果我删除它并替换为
<?php header( "refresh:5;url='https://example.com/thank-you/'" );?>
我看到空白的屏幕。
我是那种形式的新手。想了解更多,并把它做对。也许这是另一种正确的方式,来自 wordpress 本身?
【问题讨论】:
-
我必须在主题中创建 thank-you.php 而不是简单地创建一个 wordpress 页面吗?因为现在我没有这样的文件。里面必须有来自“if $mail”的 html 吗?
-
抱歉忘了这是 wordpress
-
data.php 是主题内的一个文件,所以我也可以创建 thank-you.php,但我认为客户需要该页面,因为 example.com/wp-content/themes/pzk-theme/data.php。事实上,他们在感谢页面中实现了与他们想要的相同的功能。它已经像感谢页面了。也许他们不喜欢不普通的网址,我不知道
-
我不是 WP 专家或任何接近专家的地方。所以无视我说的话
-
也许只是重命名网址?如果可以修剪它,也许也很容易更改它并按原样保留文件?
标签: php wordpress email custom-wordpress-pages