我正在为wordpress页面创建自定义表单,并且正在使用admin-post.php作为操作。 但是,每当我尝试提交表单时,我都会得到404。
下面是输出形式的代码:
function output_email_verification() {
$error = '';
if(isset($_COOKIE['rguroo_form_error'])) {
$error = $_COOKIE['rguroo_form_error'];
unset($_COOKIE['rguroo_form_error']);
}
return '<form action='.esc_url( admin_url("admin-post.php") ).'method="post">
<p class="error">'.$error.'</p>
<input type="radio" label="Purchase 12 months access" value="purchase" name="rguroo_desired_action" checked>Purchase 12 months access</input>
<input type="radio" label="Renew account" name="rguroo_desired_action" value="renewal">Renew account</input>
<input type="radio" label="Create trial account" name="rguroo_desired_action" value="trial">Create trial account</input>
<p class="form-subtext">* indicates required field</p>
<p>Email address*</p>
<input type="text" name="rguroo_email" required>
<p>Re-type email address*</p>
<input type="text" name="rguroo_email_confirmation" required>
<input type="hidden" name="action" value="rguroo_email_verification_form">
<input type="submit" value="Submit">
</form>';
}
这是我迷上了admin-post.php的动作
add_action( 'admin_post_nopriv_email_verification_form', 'verify_and_sanitize_email_form', $priority = 10, $accepted_args = 1 );
add_action( 'admin_post_email_verification_form', 'verify_and_sanitize_email_form', $priority = 10, $accepted_args = 1 );
// Email verification callback
function verify_and_sanitize_email_form() {
if(empty($_POST) || !isset($_POST['rguroo_email']) || !isset($_POST['rguroo_email_confirmation']) || !isset($_POST['rguroo_desired_action'])) {
send_form_error('There is one or more empty fields');
return;
}
$sanitized_email = sanitize_email( esc_html($_POST['rguroo_email'] ));
$sanitized_email_confirmation = sanitize_email( esc_html($_POST['rguroo_email_confirmation'] ));
$desired_action = esc_html($_POST['rguroo_desired_action']);
if(!is_email( $sanitized_email ) || !is_email( $sanitized_email_confirmation )) {
send_form_error('Email is not valid');
return;
}
if($sanitized_email !== $sanitized_email_confirmation) {
send_form_error('Emails do not match');
return;
}
if($desired_action !== 'purchase' || $desired_action !== 'renewal' || $desired_action !== 'trial') {
send_form_error('Fatal error with radio buttons');
return;
}
if(!isset($_COOKIE['rguroo_form_type'])) {
send_form_error('Server error');
return;
}
// student email verification logic
$form_type = $_COOKIE['rguroo_form_type'];
if($form_type === 'student') {
$trail = substr($sanitized_email, -4);
if($trail !== '.edu') {
send_form_error('Not a valid student email');
return;
}
// Other university specific logic here
}
setcookie('rguroo_form_action',$desired_action, 14 * DAY_IN_SECONDS);
setcookie('rguroo_form_email', $sanitized_email, 14 * DAY_IN_SECONDS);
}
// Helper. Used to output an error
function send_form_error($msg) {
setcookie('rguroo_form_error', $msg, 14 * DAY_IN_SECONDS);
}
抱歉,错误验证一团糟。 在单选按钮中选择一些内容并为两个输入字段编写“ test”和“ test”之后,我得到以下URL:
有什么想法吗?