So I have a form on one page, which is submitting to admin-post.php
, then there is a function in functions.php which is processing the form. It stores the input field data to database and then after that it should redirect the user to another page. It stores the information in database, but it is not redirecting. The problem is that it says
Cannot modify header information - headers already sent
Code:
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
<input id="user_name" type="text" name="yourname" placeholder="Name" class="form-control">
<input id="user_email" type="text" name="email" placeholder="Email" class="form-control">
<button id="button" type="button" name="submit">Go</button>
<input type="submit" value="submit" name="submit" class="btn btn-success">
<input type="hidden" name="action" value="login_form">
</form>
functions.php
function prefix_send_email_to_admin() {
/**
* At this point, $_GET/$_POST variable are available
*
* We can do our normal processing here
*/
// Sanitize the POST field
// Generate email content
// Send to appropriate email
function my_print_error(){
global $wpdb;
if($wpdb->last_error !== '') :
$str = htmlspecialchars( $wpdb->last_result, ENT_QUOTES );
$query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );
print "<div id='error'>
<p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
<code>$query</code></p>
</div>";
endif;
}
if (isset($_POST['submit'])) {
global $wpdb;
$wpdb->show_errors();
$table = 'username';
$data = array(
'name' => $_POST['yourname']
);
$success=$wpdb->insert( $table, $data );
if($success){
echo 'data has been save' ;
wp_redirect("/projects/webdev/wordpress_nepal/");
exit;
}
else {
my_print_error();
}
}
// wp_redirect("/projects/webdev/wordpress_nepal/");
}
add_action( 'wp_loaded', 'prefix_send_email_to_admin' );
I tried both, wp_loaded
hook after some googling and my original admin_post_nopriv_login_form
but it doesn't help.
So I have a form on one page, which is submitting to admin-post.php
, then there is a function in functions.php which is processing the form. It stores the input field data to database and then after that it should redirect the user to another page. It stores the information in database, but it is not redirecting. The problem is that it says
Cannot modify header information - headers already sent
Code:
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
<input id="user_name" type="text" name="yourname" placeholder="Name" class="form-control">
<input id="user_email" type="text" name="email" placeholder="Email" class="form-control">
<button id="button" type="button" name="submit">Go</button>
<input type="submit" value="submit" name="submit" class="btn btn-success">
<input type="hidden" name="action" value="login_form">
</form>
functions.php
function prefix_send_email_to_admin() {
/**
* At this point, $_GET/$_POST variable are available
*
* We can do our normal processing here
*/
// Sanitize the POST field
// Generate email content
// Send to appropriate email
function my_print_error(){
global $wpdb;
if($wpdb->last_error !== '') :
$str = htmlspecialchars( $wpdb->last_result, ENT_QUOTES );
$query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );
print "<div id='error'>
<p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
<code>$query</code></p>
</div>";
endif;
}
if (isset($_POST['submit'])) {
global $wpdb;
$wpdb->show_errors();
$table = 'username';
$data = array(
'name' => $_POST['yourname']
);
$success=$wpdb->insert( $table, $data );
if($success){
echo 'data has been save' ;
wp_redirect("http://example.design/projects/webdev/wordpress_nepal/");
exit;
}
else {
my_print_error();
}
}
// wp_redirect("http://example.design/projects/webdev/wordpress_nepal/");
}
add_action( 'wp_loaded', 'prefix_send_email_to_admin' );
I tried both, wp_loaded
hook after some googling and my original admin_post_nopriv_login_form
but it doesn't help.
1 Answer
Reset to default 1You should use the template_redirect
action instead of wp_loaded
.
It's a good practice add die();
after wp_redirect()
to.
wp_loaded
it's too early. That's what the warning is. – Jacob Peattie Commented Nov 13, 2018 at 12:15wp_loaded
should work because it firesb efore the headers are already sent which is when it's too late. But then which hook should I use? – Limpuls Commented Nov 13, 2018 at 12:17echo 'data has been save' ;
andprint "<div id='error'>
lines that are causing this specific message to appear. – Jacob Peattie Commented Nov 13, 2018 at 12:41