I am learning WordPress Plugin Development. I am Submitting a Form in Admin Panel. My form code is like below
<form method="post" action="<?php echo admin_url( 'admin-post.php?action=my_action' ); ?>" name="newAddress" id="createuser" class="validate" novalidate="novalidate">
I am catching Form submission in index.php
file using below code.
add_action('admin_post_my_action', 'my_callback_fn');
function my_callback_fn() {
echo 'hello';
}
I need Form validation message like below
I need to keep values of the Form fields with them after displaying Form validation message like below.
How can I do that ?
Some developers suggest me to use admin_init hook. But how to use that ?
I am learning WordPress Plugin Development. I am Submitting a Form in Admin Panel. My form code is like below
<form method="post" action="<?php echo admin_url( 'admin-post.php?action=my_action' ); ?>" name="newAddress" id="createuser" class="validate" novalidate="novalidate">
I am catching Form submission in index.php
file using below code.
add_action('admin_post_my_action', 'my_callback_fn');
function my_callback_fn() {
echo 'hello';
}
I need Form validation message like below
I need to keep values of the Form fields with them after displaying Form validation message like below.
How can I do that ?
Some developers suggest me to use admin_init hook. But how to use that ?
Share Improve this question edited Feb 12, 2020 at 5:31 Foysal asked Feb 12, 2020 at 3:44 FoysalFoysal 4451 gold badge5 silver badges16 bronze badges1 Answer
Reset to default 1You can store field values and error messages in session or transient and use them when (re)displaying your form.
In your callback action, if data is not valid, save error message and submitted field values. In admin_notices
hook check for saved error message. If there is one, show it as error notice. Don't forget to remove saved session/transient value.
In form builder code check session/transient for saved field values, if there are such, use them in value="..."
or as needed.
This might help.