您可以使用set_transient
使用user_id
如果您依赖于用户登录,否则,cookie可能会更好。对于已登录的用户,您可以使用user_id
在瞬态名称内设置。这里有一个例子:
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();
}
}
现在,在页面上输出表单元素之前,只需检查该用户的瞬态,如果存在,则输出错误,然后立即删除瞬态。。。
例如:
<?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>