我有一个表单发布到声明的同一页面。问题是它在提交时似乎被调用了两次。这是代码:
function user_data_form() {
add_ajax_validation();
// Form verication start
if (isset($_POST["action"])) {
switch($_POST["action"]) {
case "validate_form":
ajax_validate();
break;
case "update_user_data":
$wp_user_object = wp_get_current_user();
$id = $wp_user_object->ID;
if ( 0 == $wp_user_object->ID ) {
redirect(get_permalink(get_page_by_title( 'Sign in' )), array(
"message" => 8
));
} else {
$form_data = array(
"verified" => false,
"email" => $_POST["email"],
"first_name" => $_POST["first_name"],
"last_name" => $_POST["last_name"],
"password" => $_POST["password"],
"old_password" => $_POST["old_password"],
"verify_password" => $_POST["verify_password"]
);
$form_data["verification"] = array(
"old_password" => wp_check_password($form_data["old_password"], $wp_user_object->user_pass, $id),
"email" => validate("email", $form_data["email"]),
"password" => validate("old_password", $form_data["old_password"]),
"verify_password" => validate("verify_password", $form_data["old_password"], $form_data["verify_password"])
);
if ($form_data["verification"]["old_password"]) {
if ($form_data["email"] != $wp_user_object->user_email && $form_data["verification"]["email"]) {
invalidate_email($id, $form_data["email"]);
$message = message('success', 'Account information updated, an email has been dispatched to <strong>'.$form_data["email"].'</strong> with details on how to confirm your new email address');
$form_data["email"] = $wp_user_object->user_email;
}
if ($form_data["first_name"] != $wp_user_object->get("first_name")) {
update_user_meta($id, 'first_name', $form_data["first_name"]);
}
if ($form_data["last_name"] != $wp_user_object->get("last_name")) {
update_user_meta($id, 'last_name', $form_data["last_name"]);
}
if ($form_data["verification"]["password"]["result"] && $form_data["verification"]["verify_password"]["result"]) {
wp_set_password($form_data["password"], $id);
}
if (!message) {
$message = message('success', 'Account information updated');
}
} else {
$message = message('error', '<b>Error:</b> Incorrect password');
}
}
break;
}
}
// Form verication end
// Create form
if (!$form_data["verified"]) {
global $current_user;
get_currentuserinfo();
$new_email = get_metadata("user", $current_user->user_ID, "new_email", true);
$email_expires = get_metadata("user", $current_user->user_ID, "email_expires", true);
$form = 'You can view and update your account settings using the following form:
<form action="#" method="post" class="user-data">
'.$message.'
<input type="hidden" name="action" value="update_user_data" />
<div class="fields">
<div class="field">
<label for="email">Email:</label>
<input type="text" name="email" id="email" class="text" data-validation-method="email" class="text" data-validation-ignore="'.encode($current_user->user_email).'" value="'.(($form_data["email"]) ? $form_data["email"] : $current_user->user_email).'" placeholder="youremail@somesite.com" data-validation-response="'.(($form_data["email"] == $current_user->user_email) ? NULL : encode($form_data["verification"]["email"])).'" />
'.(($new_email) ? '<span class="fail">An email has been sent to the above address to change email to <strong>'.$new_email.'</strong></span>' : NULL).'
<span class="info">You will recieve a validation email to your new email address. If you don\'t confirm within 24 hours your email address will revert to the last validated one.</span>
</div>
<div class="field">
<label for="first_name">First name: <sup>1</sup></label>
<input type="text" name="first_name" id="first_name" class="text" value="'.$current_user->user_firstname.'"/>
<label for="last_name">Last name: <sup>1</sup></label>
<input type="text" name="last_name" id="last_name" class="text" value="'.$current_user->user_lastname.'"/>
<span class="info"><sup>1</sup> Required to issue certificates.</span>
<div class="fieldgroup">
<span class="info">
<strong>Note:</strong> Only fill in these fields if you wish to change your password.
</span>
<div class="field">
<label for="password">New password:</label>
<input type="password" name="password" id="password" data-validation-method="password" class="text" value="'.$form_data["password"].'" data-validation-response="'.((strlen($form_data["password"]) > 0) ? encode($form_data["verification"]["password"]) : NULL ).'" />
</div>
<div class="field">
<label for="verify_password">Retype your new password:</label>
<input type="password" name="verify_password" id="verify_password" class="text" data-validation-method="verify_password" data-validation-requires="password" value="'.$form_data["verify_password"].'" data-validation-response="'.((strlen($form_data["verify_password"]) > 0) ? encode($form_data["verification"]["verify_password"]) : NULL ).'" />
</div>
</div>
<div class="field">
<label for="password">Current password:</label>
<input type="password" name="old_password" id="old_password" class="text" value=""/>
<span class="info">Your current password is required to make changes to your account information. Have you <a href="'.get_permalink( get_page_by_title( 'Reset your password' ) ).'">forgotten your password?</a></span>
</div>
</div>
<input type="submit" id="submitbtn" class="button omega" name="submit" value="Update my account information" />
</div>
</form>';
} else {
redirect(get_permalink(get_page_by_title( 'My account' )), array(
"message" => 2
));
}
return $form;
}
问题是invalidate_email()
被调用了两次。虽然它只应在$_POST["action"] == "update_user_data"
时触发,而invalidate_email()
只应在提交表单之后触发。我无法确定第二个电话的来源。
这是我的function invalidate_email($user_id, $new_email) {
$key = createKey($new_email);
$activation_url = get_permalink(get_page_by_title( 'Activate' ))."?key=".$key."&id=".$user_id."&type=email";
update_user_meta($user_id, 'new_email', $new_email);
update_user_meta($user_id, 'email_activation_key', $key);
$expires = get_metadata("user", $user_id, "email_expires", true);
if (!$expires) {
$expires = time() + 86400000; //86400000 milliseconds = 24 Hours
update_user_meta($user_id, 'email_expires', $expires);
}
$message = "A request to update your email for your account on First steps has been made, please confirm your email address by clicking the following link: <p />";
$message .= "<a href='".$activation_url."'>".$activation_url."</a> <p />";
$message .= "If you did not request the change your account may be comprimised and we recommend you change your password from the 'my account' page. <p />";
$message .= "If you do not confirm your new email address within 24 hours the request will expire and you will have to request a new one.";
email($new_email, "First steps - Confirm your new email address", "Confirm email address change", $message);
}
功能:
email()
和function email($to, $subject, $heading, $message, $attachments = NULL) {
$headers = array('From: First steps <info@rcnhca.org.uk>', 'Content-type: text/html');
$body = '<table width="100%" style="background:#ECECEC;" cellpadding="0" cellspacing="0">
<tr width="100%" style="background: #3393b5;">
<td align="center">
<table width="500" cellpadding="0" cellspacing="0">
<tr>
<td align="center" style="height:45px; vertical-align:middle;">
<a href="'.get_bloginfo("url").'"><img alt="First steps for health care assistants" src="'.get_stylesheet_directory_uri().'/library/images/logo.png" width="347" height="32" style="vertical-align:middle;display:block;" /></a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" style="padding: 10px 0px;">
<table width="500" style="padding: 10px;background:#FFFFFF;border:1px solid #BCBCBC;margin:0px 10px;" cellpadding="0" cellspacing="0">
<tr>
<td style="color:#000000;font-size:14px;line-height:1.5em;text-align:left;"><font face="verdana">
<h2 style="margin-top: 0.1em; font-size: 18px; padding-bottom: 0.5em; color: #333333; border-bottom: 1px solid #CCCCCC; margin-bottom: 0.7em;">'.$heading.'</h2>
'.$message.'
</font></td>
</tr>
</table>
<table width="500">
<tr>
<td style="font-size:12px;line-height:1.5em;text-align:left;"><font face="verdana">We are committed to keeping your email private, we do not share your address with third-parties.</font></td>
</tr>
</table>
</td>
</tr>
</table> ';
wp_mail($to, $subject, $body, $headers, $attachments);
}
:
add_ajax_validation()
我很肯定它不是Ajax调用(所以忽略HTTP_X_REQUESTED_WITH
),因为即使我删除了Ajax验证(这是这个函数的作用),它也会发生。我还检查了应该存在的debug_backtrace()
的存在,因为我的所有Ajax都是由jQuery执行的,标题应该存在。
编辑:回复@Oli
我执行了$trace= debug_backtrace();
$funcs = array();
foreach ($trace as $func) {
array_push($funcs, $func['function']);
}
$funcs = implode('\n', $funcs);
以查看调用该函数的内容,这是我的回溯:
invalidate_email
user_data_form
call_user_func
do_shortcode_tag
preg_replace_callback
do_shortcode
pre_process_shortcode
call_user_func_array
apply_filters
wp_trim_excerpt
call_user_func_array
apply_filters
get_the_excerpt
require_once
load_template
locate_template
get_header
include
require_once
require
输出1:
invalidate_email
user_data_form
call_user_func
do_shortcode_tag
preg_replace_callback
do_shortcode
pre_process_shortcode
call_user_func_array
apply_filters
the_content
include
require_once
require
输出2:
call_user_func()
在add_shortcode('user_data_form', 'user_data_form', 134);
及以后,这是由WordPress CMS完成以下函数调用的结果:{{1}}
答案 0 :(得分:0)
调查 var_dump(func_get_args())
以调查并检测您在invalidate_email
函数中收到的参数。
答案 1 :(得分:0)
我之前用mail()注意到了这一点。当你在header部分有一个“To”和mail()的第一个参数中的“to”变量时,它会被发送两次。您可能想删除标题的部分并尝试使用。
应该仔细阅读这个问题。
答案 2 :(得分:0)
我觉得这样一个nincompoop但我只是意识到这是自我造成的。我有这段代码试图避免WordPress的自动格式化函数wpautop
。它似乎会解雇他们两次。
function pre_process_shortcode($content) {
global $shortcode_tags;
// Backup current registered shortcodes and clear them all out
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array();
add_shortcode('registration_form', 'registration_form', 134);
add_shortcode('login_form', 'login_form', 134);
add_shortcode('user_data_form', 'user_data_form', 134);
add_shortcode('activation_code', 'activation_code', 134);
add_shortcode('logout_code', 'logout_code', 134);
add_shortcode('reset_password_form', 'reset_password_form', 134);
// Do the shortcode (only the one above is registered)
$content = do_shortcode($content);
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;
return $content;
}
add_filter('the_content', 'pre_process_shortcode', 13);
我将此更改为:
add_shortcode('registration_form', 'registration_form', 134);
add_shortcode('login_form', 'login_form', 134);
add_shortcode('user_data_form', 'user_data_form', 134);
add_shortcode('activation_code', 'activation_code', 134);
add_shortcode('logout_code', 'logout_code', 134);
add_shortcode('reset_password_form', 'reset_password_form', 134);
这当然会产生其他问题,但至少它解决了这个问题,我会找到另一种方法来做我想做的事情。对于那些花时间尝试解决这个问题的人来说,对不起,因为你从来没有从我提供的信息中解决这个问题。