使用admin-post.php的表单在提交后提供404

时间:2019-06-19 作者:Eli

我正在为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>\';
}
这是我挂在管理帖子上的动作。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);
}
很抱歉错误验证的混乱。

有什么想法吗?

2 个回复
SO网友:Krzysiek Dróżdż

首先

你的action 被称为rguroo_email_verification_form, 因此,您应该在注册挂钩时使用它。但您可以使用此选项:

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 );
它应该是:

add_action( \'admin_post_nopriv_rguroo_email_verification_form\', \'verify_and_sanitize_email_form\', $priority = 10, $accepted_args = 1 );
add_action( \'admin_post_rguroo_email_verification_form\', \'verify_and_sanitize_email_form\', $priority = 10, $accepted_args = 1 );
还有。。。这些操作不采用任何参数,但您声明它们应采用一个参数(但稍后,您的函数不采用任何…)

SO网友:Eli

正如Krzysiek所指出的,表单中的action字段和admin post挂钩之间存在不一致。

也就是说,404问题的真正来源和原因是格式错误。

<form action=\'.esc_url( admin_url("admin-post.php") ).\'method="post">
正在输出

<form action="https://rguroo.com/wp-admin/admin-post.phpmethod=post">
正确的标签如下:

<form action="\'.esc_url( admin_url("admin-post.php") ).\'" method="post">
我是个白痴。。。

相关推荐

Dealing with html forms

I want to work with html form but i cant understand the Action method and when we need to use it.For example found this code on w3schools:<form action=\"action_page.php\"> First name:<br> <input type=\"text\" value=\"Mickey\">&#