0

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

enter image description here

I need to keep values of the Form fields with them after displaying Form validation message like below.

enter image description here

How can I do that ?

Some developers suggest me to use admin_init hook. But how to use that ?

1 Answer 1

1

You 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.