最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Catching Form Submission in WordPress Admin Panel

programmeradmin1浏览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

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 badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论