[英]How to add php to wordpress form
我很難將 php 代碼集成到我在 wordpress 上創建的表單中。我嘗試了 3 種不同的 php 插件但無濟於事。 這是 html 和 css。有誰知道我應該將 php 放在 wordpress 上的什么位置,以及我應該如何通過表單中的“操作”正確調用它?
css:
<style>
input[type=text], input[type=number], select[name=province]{ font-family: arial; width:100%;
border: 1px solid #E5E5E5; padding: 10px 20px;}
input[name=ffirstname] {width:49%; margin-right:1%; }
input[name=lastname] {width:49%; margin-left:1%; }
input[name=address] {width:65.6667%; margin-right:1%; }
input[name=unit] {width:32.3337%; margin-left:1%; }
input[name=city] {width:49%; margin-right:1%; }
select[name=province] {width:24%; margin-left:1%;}
input[name=postal] {width:24%; margin-left:1%; }
input[name=email] {width:49%; margin-right:1%; }
input[name=phone] {width:49%; margin-left:1%;}
input[class=submit] {
background-color: #f05a28;
color: white;
padding: 8px 20px;
margin-left: 85%;
border-radius: 4px;
border: none; !important;
outline: none; !important ;
cursor: pointer;
box-shadow: none; !important;
}
</style>
html:
<form name"infoform" method="post" action="form-to-email.php" >
<br><input type="text" name="ffirstname" placeholder="First Name"/><input type="text" name="lastname" placeholder="Last Name"/>
<br><br><input type="text" name="address" placeholder="Address"/><input type="text" name="unit" placeholder="Unit"/></br>
<br><input type="text" name="city" placeholder="City"/><select name="province" form="form1">
<option value="ab">AB</option>
<option value="BC">BC</option>
<option value="BC">MB</option>
<option value="NB">NB</option>
<option value="NL">NL</option>
<option value="NS">NS</option>
<option value="ON">ON</option>
<option value="PE">PE</option>
<option value="QC">QC</option>
<option value="SK">SK</option>
</select><input type="text" name="postal" placeholder="Postal Code"/></br>
<br><input type="text" name="country" placeholder="Country"/></br>
<Br><input type="text" name="email" placeholder="Email"/><input type="number" name="phone" placeholder="Phone#"/></br>
<br> <br><input type="submit" value="Next"/>
php:
<?php
$name = $_POST['ffirstname'];
$email = $_POST['lastname'];
$message = $_POST['address'];
$to = "joeblow@hotmail.com";
$subject = "My contact form";
$body = " You have received a new";
mail($to,$subject,$body);
?>
通常我使用footer.php
添加 JS 和 PHP 代碼,就在正文的結束標記之前。 但如果這不起作用,請嘗試使用header.php
。 也許您的代碼需要在結束之前包含在內。 您可以毫無問題地將 HTML 部分放入頁面或發布。
如果您想使用插件,可以使用Contact form 7輕松實現。
更新 - 將 PHP 代碼添加到 Wordpress 中:
您可以在<?php
標簽之間添加代碼,因此打開您的header.php
、 footer.php
或page.php
並在標簽內寫入您的代碼
<?php
//PHP Code here
?>
這樣,您將在您網站上的每個 Wordpress 頁面中看到此代碼。
Header 和頁腳始終包含在所有頁面上。 如果您只在帖子或頁面中需要它,請使用page.php
或post.php
如果您想控制 php 在 Wordpress 頁面中的位置,請查看頁面模板。 您可以創建一個表單頁面模板,其中包含表單和必要的 php 來處理它,然后將其應用於給定的 url 例如/sign-up
。 這樣,您就不必讓表單代碼弄亂每個頁面的header.php
或footer.php
。
對於表單本身,您可以讓它自己post
,然后使用isset($_POST['field_name'])
檢查頁面加載時是否設置了表單變量。 如果它們存在,那么您可以假設表單已發布,因此您需要發送 email,否則如果它們不存在,那么您可以假設您需要顯示該表單。
例如
<?php if(isset($_POST['ffirstname'])){
// Do some appropriate sanitization for your use case
$ffirstname = htmlspecialchars(trim($_POST['ffirstname']));
//.. repeat for other fields, and then send email
// could drop out of php here and show a thank you message
?>
<p>Thank you for submitting the form</p>
<?php
} else {
// $_POST['ffirstname'] not set, so show form ?>
<form method="post" action="">
<input type="text" name="ffirstname" />
<input type="submit" value="Submit" />
</form>
<?php } ?>
如果你想為此連接到 Wordpress,那么一種方法是添加一個名為 action 的隱藏輸入,並將其值設置為functions.php
文件中 function 的名稱。 您還需要添加一些 javascript 以將 ajax 提交到 function 然后可以執行邏輯和 email 發送:
例如
// Form Page
<form method="post" action="" id="emailForm">
<input type="text" name="ffirstname" />
<input type="hidden" name="action" value="sendEmail" />
<input type="submit" value="Submit" />
</form>
// Somewhere further down the same page as the form
<script type="text/javascript">
jQuery('#emailForm').on('submit', function() {
e.preventDefault();
jQuery.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
data: jQuery('#emailForm').serialize(),
success: function(data) {
// Perhaps some UI update here to let the user know
// the submission was successful
},
error: function(error) {
// Do something with the error
}
});
});
</script>
然后在你的主題functions.php
文件中:
// Functions.php
function sendEmail(){
// Do some appropriate sanitization for your use case
$ffirstname = htmlspecialchars(trim($_POST['ffirstname']));
//.. repeat for other fields as necessary, and then send email
return wp_mail($recipient, $subject, $body, $headers);
}
add_action('wp_ajax_sendEmail', 'sendEmail');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.