最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Handling error states with admin_post

matteradmin9PV0评论

I have a front-end form posting to admin_post. Here, I do some validation. If there are errors, I want to show an error message for the relevant fields.

However, I don't know how to redirect back to the submission page and retain the field values.

Currently I'm using the below:.

add_action( 'admin_post_form', 'form_post' );
function form_post() {
  $validate = // Validation functions go here
  if ($validate == 'error') {
    $url = wp_get_referer();
    $url = add_query_arg( 'error', 'email', $url );
    wp_redirect( $url );
    die();
  }
}

But this (naturally) doesn't retain the filled form field values, so the user has to start all over again.

If I were posting to the same page as the form this would be simple - just set the value as <input value="<?php echo $_POST['email']; ?>"> but I'm not sure how to achieve the same effect using admin-post.

Can you help?

I have a front-end form posting to admin_post. Here, I do some validation. If there are errors, I want to show an error message for the relevant fields.

However, I don't know how to redirect back to the submission page and retain the field values.

Currently I'm using the below:.

add_action( 'admin_post_form', 'form_post' );
function form_post() {
  $validate = // Validation functions go here
  if ($validate == 'error') {
    $url = wp_get_referer();
    $url = add_query_arg( 'error', 'email', $url );
    wp_redirect( $url );
    die();
  }
}

But this (naturally) doesn't retain the filled form field values, so the user has to start all over again.

If I were posting to the same page as the form this would be simple - just set the value as <input value="<?php echo $_POST['email']; ?>"> but I'm not sure how to achieve the same effect using admin-post.

Can you help?

Share Improve this question asked Dec 14, 2017 at 21:28 Kevin RobinsonKevin Robinson 617 bronze badges 1
  • PS I could pass the data in the query string and use $_GET but I'd rather not pass sensitive data in the url – Kevin Robinson Commented Dec 14, 2017 at 21:42
Add a comment  | 

3 Answers 3

Reset to default 1

You can use set_transient with the user_id if you are relying on users being logged in, otherwise, cookie would probably be better. For logged in users, you can have the user_id get set within the name of the transient. An example here:

function form_post() {
    $user_info = wp_get_current_user();
    $user_id = $user_info->exists() && !empty($user_info->ID) ? $user_info->ID : 0;
    $url = wp_get_referer();

    // Validate the form fields and populate this array when errors occur
    $validation_errors = array();  // I like to use the id of the elements as keys and the error string as the values of the array, so i can add error class to the elements if needed easily...

    // Now to save the transient...
    if (!empty($validation_errors)) {
        set_transient('validation_errors_' . $user_id, $my_errors);
        wp_redirect($url);
        die();
    }
}

Now before you output your form element on your page, you just need to check the transient for that user, if exists, output the errors, than delete the transient right after that...

For example:

<?php 
$user_info = wp_get_current_user();

$user_id = $user_info->exists() && !empty($user_info->ID) ? $user_info->ID : 0;
$validation_errors = get_transient('validation_errors_' . $user_id);

if ($validation_errors !== FALSE) {
   echo '<div class="errors">Please fix the following Validation Errors:<ul><li>' . implode('</li><li>', $validation_errors) . '</li></ul></div>';
   delete_transient('validation_errors_' . $user_id);
} ?>

<form ...>

</form>

I solved this using the $_SESSION variable to pass data to the following page.

  if ($validate == 'error') {
    $_SESSION['formstatus'] = 'error';
    $_SESSION['formdata'] = $_POST;
    $url = wp_get_referer();
    wp_redirect( $url );
    die();
  }

And

<input name="email" value="<?php echo $_SESSION['formdata']['email']">

Finally, clear the data at the bottom of the page, after we've used it...

$_SESSION['formstatus'] = $_SESSION['formdata'] = null;

You should just pass all the arguments. This obviously not a great solution when trying to pass them on the URL, so use of cookies might be a much better solution.

But frankly, on a modern web site you should just use AJAX, report and handle the errors on browser side and also redirect from it.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far