-
-
Save 5ally/74c142e0ce0d0bafa643b6cf7c933acb to your computer and use it in GitHub Desktop.
See https://wordpress.stackexchange.com/q/360588 for details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function updated_tos_agree_submit() { | |
$errors = array(); | |
// Validate the submitted form data. | |
if ( empty( $_POST['tos_agree'] ) ) { | |
$errors['tos_agree'] = 'You must agree to the Terms.'; | |
} | |
if ( ! empty( $errors ) ) { | |
// Store the errors in a transient - a temporary option in the database. | |
set_transient( 'my_form_errors', $errors, 30 ); | |
set_transient( 'my_form_data', array( | |
// Put relevant submitted form data here. | |
), 30 ); | |
} else { | |
// Update User Meta here. | |
// Delete the errors transient and add a success one. | |
delete_transient( 'my_form_data' ); | |
delete_transient( 'my_form_errors' ); | |
set_transient( 'my_form_success', 'true', 30 ); | |
} | |
// Redirect back to the form page. | |
wp_redirect( wp_get_referer() ); | |
exit; | |
} | |
add_action( 'admin_post_nopriv_updated_tos_agree', 'updated_tos_agree_submit' ); | |
add_action( 'admin_post_updated_tos_agree', 'updated_tos_agree_submit' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This is false if there has been no submission (i.e. the request method | |
// is GET) or that there are form validation errors (from admin-post.php). | |
$settings_updated = false; | |
// Get the form errors, if any. | |
$errors = get_transient( 'my_form_errors' ); | |
// Then display the errors above the form or wherever else that you prefer. | |
if ( is_array( $errors ) && ! empty( $errors ) ) { | |
echo '<ul class="form-errors">'; | |
echo '<li>' . implode( '</li><li>', $errors ) . '</li>'; | |
echo '</ul>'; | |
} elseif ( ! empty( get_transient( 'my_form_success' ) ) ) { | |
$settings_updated = true; | |
delete_transient( 'my_form_success' ); | |
} | |
if ( ! $settings_updated ) : | |
$data = get_transient( 'my_form_data' ); | |
// So you'd use $data['<name>'] instead of $_POST['<name>'] to get the submitted data. | |
?> | |
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post"> | |
<label for="tos_agree"> | |
<input type="checkbox" name="tos_agree" id="tos_agree" | |
<?php checked( true, ! empty( $data['tos_agree'] ) ); ?> /> | |
Some text for the checkbox | |
</label> | |
<button type="submit" name="agree" value="I Agree">I Agree</button> | |
<input name="action" type="hidden" value="updated_tos_agree" /> | |
</form> | |
<?php else : ?> | |
<p>Settings updated.</p> | |
<?php endif; // end $settings_updated ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment