• 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

    https://i.stack.imgur.com/Ekxsh.png

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

    https://i.stack.imgur.com/jy74Q.png

    How can I do that ?

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

    • This topic was modified 4 years, 9 months ago by abufoysal.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator bcworkz

    (@bcworkz)

    You don’t really need “admin_init”. It fires for all admin requests, you only need to handle your own form events. If you want to use admin-post.php, you’d need to recreate your entire form as part of the action callback. Or use admin-post.php from the start. I suggest you handle everything through the original form code, where ever it is, allowing the form to submit to its own page. You can distinguish the initial GET request from form submittal requests by values in $_POST or from $_SERVER[‘REQUEST_METHOD’].

    There are different types of form validation. HTML forms will validate for themselves to a limited extent. JavaScript can validate before submittal, but don’t rely upon it for critical validation. It’s possible for users to spoof or disable JS validation. PHP validation is secure but of course requires communication back and forth.

    If you submit data via Ajax, the form field values will be preserved for the session. Under some conditions field values are preserved by the browser for the current session. For longer term preservation, get the saved values from the DB while generating the form from the start. The code needs to anticipate that there might not be any saved data and still execute without throwing any errors.

    Thread Starter abufoysal

    (@abufoysal)

    Thanks @bcworkz for your reply. I am submitting the Form in same page. My code is like below

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //some validation code here
        if(isset($name) && isset($email)) {
          //submit the Form & save data in Database here
          // I would like to redirect here to another page. How can I do that ?
        }
        else {
          return false;
        }
     }
    • This reply was modified 4 years, 9 months ago by abufoysal.
    Moderator bcworkz

    (@bcworkz)

    As long as no output has occurred prior to redirection, you can redirect with wp_redirect(), which sends a Location: header.

    Thread Starter abufoysal

    (@abufoysal)

    Thanks @bcworkz for your reply. I used below code with wp_redirect().

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //some validation code here
        if(isset($name) && isset($email)) {
          //submit the Form & save data in Database here
          wp_redirect('https://facebook.com');
          exit;
        }
        else {
          return false;
        }
     }

    I am getting below error.

    View post on imgur.com

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    “Headers already sent” is misleading. The actual issue is HTML content output had already begun. To redirect in PHP, it most be done earlier than any output. It’s not clear where your code fits within the overall WP execution, but it’s clearly too late to redirect through PHP. Your code needs to execute earlier or the redirect needs to be accomplished through JavaScript.

    Thread Starter abufoysal

    (@abufoysal)

    Thanks @bcworkz. Previously I put below code after HTML Form code

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //some validation code here
        if(isset($name) && isset($email)) {
          //submit the Form & save data in Database here
          wp_redirect('https://facebook.com');
          exit;
        }
        else {
          return false;
        }
     }

    Now I put above code before HTML Form code.

    But I am getting Same Error. How can I solve the Error ?

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    If your code is on any theme template, it’s too late to redirect. Well, you might try the header.php template before any output occurs, it might be early enough. Once the initial <!DOCTYPE html> is output it’s too late. If that doesn’t work, you will need to hook an action that fires early enough and execute your code from there. Typical actions in approximate order are listed at https://codex.www.ads-software.com/Plugin_API/Action_Reference

    Thread Starter abufoysal

    (@abufoysal)

    Thanks @bcworkz. I am working in Admin Panel. May be there is no theme template. I am attaching a screenshot. May be you can get idea from this screenshot.

    View post on imgur.com

    Moderator bcworkz

    (@bcworkz)

    Admin redirects are difficult because WP outputs its menus before your plugin’s code is able to do anything. You could redirect from the “admin_init” action. This fires for all admin requests so ensure you are only redirecting for the correct request.

    As mentioned earlier. the alternative is using JavaScript to redirect from your admin screen.

    Thread Starter abufoysal

    (@abufoysal)

    Thanks @bcworkz . Could you please help me with some sample code with admin_init ? Thanks.

    Moderator bcworkz

    (@bcworkz)

    Example action hooks are found here. Just use 'admin_init' action instead of the example hook. Your callback should verify the request is for your form submittal before trying to do anything. After the redirect, the action will fire again (if redirected to an admin page). If you were to still redirect at that point you’ll start an infinite redirect loop.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Form Submission in WordPress Admin Panel’ is closed to new replies.