网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

WordPress插件在使用wp_redirect时抛出警告

GG网络技术分享 2025-03-18 16:12 6


问题描述:

I am developing a plugin where user will submit a form. The form submits to the same page. Form data handling code is implemented by checking the condition-

if ( !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'customer_add_new\')

Then the data is validated and entered to database using $wpdb->insert.

After that, I want to pass a variable to the URL, so that success message can be displayed and user can be stopped from re-submitting the form by refreshing the page. For that, I used-

$url = add_query_arg( array(

\'customer_add_new\' => \'success\'

) );

wp_redirect( $url );

exit();

But this code is throwing the following error-

Warning: Cannot modify header information - headers already sent by (output started at E:\\wamp\\www\\cp_plugin\\wp-includes\\class.wp-scripts.php:343) in E:\\wamp\\www\\cp_plugin\\wp-includes\\pluggable.php on line 1216

I have successfully used the same code in the theme for this very same project. But the functionality was better suited for a plugin, than for a theme.

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我正在开发一个用户将提交表单的插件。 表单提交到同一页面。 表单数据处理代码通过检查条件 - </ p>

  if(!empty($ _POST [\'action\'])&amp;&amp; $ _POST [\'action\'] =来实现 =\'customer_add_new\')

</ code> </ pre>

然后使用$ wpdb-&gt; insert验证数据并输入数据库。</ p>

之后,我想将一个变量传递给URL,这样就可以显示成功消息,并且可以通过刷新页面来停止用户重新提交表单。 为此,我使用了 - </ p>

  $ url = add_query_arg(array(

\'customer_add_new\'=&gt;\'success\'

));

wp_redirect($ url);

exit();

</ code> </ pre>

但此代码抛出以下错误 - </ p>

 警告:无法修改标题 信息 - 已在E:\\ wamp \\ www \\ cp_plugin \\ wp-includes \\ pluggable.php中发送的输出(输出从E:\\ wamp \\ www \\ cp_plugin \\ wp-includes \\ class.wp-scripts.php:343开始) 在第1216行

</ code> </ pre>

我已成功在主题中为同一个项目使用了相同的代码。 但是功能更适合插件而不是主题。</ p>

</ div>

网友观点:

hook admin_post will help you. Works the same as admin-ajax.php.

Change form action to <?php echo admin_url(\'admin-post.php\'); ?> and create hooks:

add_action( \'admin_post_{action}\', \'funct\' );

add_action( \'admin_post_nopriv_{action}\', \'funct\' );

function funct() {

if ( wp_verify_nonce( \'some_nonce\', \'some_action\' ) ) {

// Your code using $_POST

// And create wp_redirect();

}

}

This might help you.

标签:

提交需求或反馈

Demand feedback