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.
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 6 years, 3 months ago by
abufoysal.
As long as no output has occurred prior to redirection, you can redirect with wp_redirect(), which sends a Location: header.
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.
“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.
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.
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.ww.wp.xz.cn/Plugin_API/Action_Reference
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
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.
Thanks @bcworkz . Could you please help me with some sample code with admin_init ? Thanks.
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.