admin管理员组文章数量:1130349
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.
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.
-
You can't print output at
wp_loadedit's too early. That's what the warning is. – Jacob Peattie Commented Nov 13, 2018 at 12:15 -
@JacobPeattie As I understand,
wp_loadedshould 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:17 - No, the problem is outputting anything before the headers are sent. WordPress is trying to send headers but there's already been output to the screen, which messes it up. – Jacob Peattie Commented Nov 13, 2018 at 12:25
- Then how do I redirect before headers are sent? – Limpuls Commented Nov 13, 2018 at 12:26
-
A redirect isn't output, they've got nothing to do with each other. It's the
echo '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
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.
本文标签: phpCannot modify header informationheaders already sent
版权声明:本文标题:php - Cannot modify header information - headers already sent 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749185623a2329337.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp_loadedit's too early. That's what the warning is. – Jacob Peattie Commented Nov 13, 2018 at 12:15wp_loadedshould 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