我在Wordspress网页上有一个动态的联系表格。
这是HTML结构:
<div class="form_content">
<div> Name: <span> <input type="text" id="fullname"> </span></div>
<div> E-mail: <span> <input type="text" id="email"> </span></div>
<div> Message: <span> <input type="text" id="text"> </span></div>
<div id="sendBtn"> Submit </div>
</div>
我有从HTML中获取输入的函数,并将它们发送到我的二十五子主题上的content.php文件。
// Send button for the "contact form".
jQuery('#sendBtn').click(function(){
//get info
var fullname = jQuery("#fullname").val();
var email = jQuery("#email").val();
var text = jQuery("#text").val();
//send info to php
jQuery.ajax({
beforeSend: function() {
if ( IsEmail(email) == false) {
jQuery('#aboutUnsuccess').show("slow");
/*jQuery('#pixel-thing').show("fast");*/
jQuery('.form_content').hide("slow");
}
},
url: 'http://54.149.xx.xx/wp-content/themes/twentyfifteen-child/contact.php',
type: "POST",
data: ({ "fullname": fullname, "email": email, "text": text }),
success: function (results){
if ( IsEmail(email) == true) {
//hide table
jQuery('.form_content').hide('slow', function() {
jQuery('.form_content').hide( "slow" );
});
//show textboxes
jQuery('#aboutSuccess').show("slow");
jQuery( "#aboutSuccess" ).append( "<iframe id=\"pixel-thing\" src=\"http://54.149.xx.xx/wp-content/themes/twentyfifteen-child/thePixel.html\" width=\"1\" height=\"1\" border=\"0\"></iframe>" );
/************************************/
window.google_trackConversion({
google_conversion_id: 666,
google_remarketing_only: true
});
/*************************************/
}
}
});
最后我的content.php处理参数并将相关信息发送到我的电子邮箱:
<?php
require_once "Mail.php";
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "info@example.com";
$host = "ssl://smtp.gmail.com:465";
$username = 'myEmail@email.com';
$password = 'passpaspaspas';
$email_subject = "You have a new email from $email via example.com website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($email_to, $headers, $message);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}
}
我正在将没有任何MVC / CMS构建的网站移动到wordpress。这适用于我的常规&#34;网站。
为什么我的代码不能在WordPress中运行?
这是Chromes&#34; Network&#34;:
的输出Remote Address:54.159.xx.xx:80
Request URL:http://54.159.xx.xx/wp-content/themes/twentyfifteen-child/contact.php
Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:63
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:wp-settings-1=editor%3Dtinymce%26posts_list_mode%3Dlist; wp-settings-time-1=1424359234
Host:54.149.xx.xx
Origin:http://54.149.xx.xx
Referer:http://54.149.xx.xx/?page_id=73
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
fullname: Test
email:Test@gmail.com
text:This is a test
Response Headersview source
Connection:close
Content-Length:0
Content-Type:text/html
Date:Tue, 24 Feb 2015 14:13:46 GMT
Server:Apache/2.4.7 (Ubuntu)
X-Powered-By:PHP/5.5.9-1ubuntu4.5
答案 0 :(得分:1)
Wordpress不喜欢你直接发布到主题中的php文件。您应该做的是发布到/(或您要发布的任何模板/路径,比如“联系”),并将表单处理代码放在主题文件中。
我建议将您的代码放在functions.php中,并添加一个动作来监听ajax请求,有关详细说明,请参阅wp_ajax_(action) in the Wordpress Codex。
- 编辑 -
如果您想快速上手(不是我们所有人),此代码段几乎可以处理您想要的所有内容,将其放入functions.php
并从您的javascript调用/wp-admin/admin-ajax.php
。在ajax调用中,请确保添加变量action
并将其填充my_action
以使此示例正常工作。
add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
$data['output'] = $_REQUEST['username'];
echo json_encode($data);
wp_die();
}