提交表单
-
-
很难从此信息中猜测任何内容,您是否尝试过调试以获取更多详细信息?[更好的HTTP重定向](http://wordpress.org/extend/plugins/better-http-redirects/)插件是研究重定向问题的好工具.Hard to guess anything from this information, have you tried to debug it for more details? [Better HTTP Redirects](http://wordpress.org/extend/plugins/better-http-redirects/) plugin is good tool to look into redirect issues.
- 2
- 2012-12-22
- Rarst
-
请在上下文中发布此代码.Please post this code in context.
- 0
- 2012-12-22
- s_ha_dum
-
@s_ha_dum我已经更新了我的问题,以包含粘贴框@s_ha_dum I've updated my question to include a pastebin
- 0
- 2012-12-22
- Anagio
-
@Rarst我已使用整个代码的pastebin更新了问题@Rarst I've updated the question with a pastebin of the entire code
- 0
- 2012-12-22
- Anagio
-
@Rarst我安装了插件,请查看更新后的帖子,它显示302并链接到新帖子,但此处没有刷新@Rarst I installed the plugin please see my updated post it displays a 302 and links to the new post but doesn't refresh there
- 0
- 2012-12-22
- Anagio
-
2 个回答
- 投票数
-
- 2012-12-22
在将内容发送到浏览器之前,您只能使用
wp_redirect
.如果要启用php调试,则由于第一行的get_header()
会出现"已发送标头"错误.您可以执行更早的操作,而不是处理模板中的表单,就像
wp_loaded code>,如果您只是要重定向,则将一些查询保存到数据库中.
编辑,例如-
add_action('wp_loaded','wpa76991_process_form'); 函数wpa76991_process_form(){ if(isset($ _POST ['my_form_widget'])): //处理表单,然后 wp_redirect(get_permalink($pid)); 出口(); 万一; }
使用操作,您可以使代码与模板保持隔离.将此代码与短代码结合在一起以输出表单,并将其全部包装在一个类中,以保存处理/输出之间的状态,而无需触摸前端模板即可完成所有操作.
You can only use
wp_redirect
before content is sent to the browser. If you were to enable php debugging you'd see a "headers already sent" error due toget_header()
on the first line.Rather than process the form in the template, you can hook an earlier action, like
wp_loaded
, and save some queries to the db if you're just going to redirect away.EDIT, example-
add_action( 'wp_loaded', 'wpa76991_process_form' ); function wpa76991_process_form(){ if( isset( $_POST['my_form_widget'] ) ): // process form, and then wp_redirect( get_permalink( $pid ) ); exit(); endif; }
Using an action, you can keep the code out of and separated from your templates. Combine this with a shortcode to output the form and wrap it all in a class to save state between processing/output, and you could do it all without touching the front end templates.
-
@Miloe是的,我刚刚看到标头已经发送了消息,并且启用了调试功能,并且启用了更好的http重定向插件.我对如何使用钩子不熟悉,请问我可以参考一些教程或显示一些示例代码吗?@Miloe yes I just saw the headers already sent message now with debugging enabled and the better http redirect plugin on. I'm not familiar with how to use the hooks, can you point me to some tutorial or show some example code please
- 0
- 2012-12-22
- Anagio
-
@Anagio-添加了一个示例@Anagio - added an example
- 0
- 2012-12-22
- Milo
-
谢谢,所以您的建议是将表单放入短代码中,然后在模板中使用do_shortcode()来显示表单.该钩子将进入我的functions.php.表单的作用是什么触发功能/挂钩?Thanks, so your suggesting I put the form into a short code then use do_shortcode() within the template to display the form. The hook would go into my functions.php. What does the action of the form become to fire the function/hook?
- 0
- 2012-12-23
- Anagio
-
您不必使用`do_shortcode`,我的意思是,您可以通过简码将其添加到帖子/页面的内容中,然后将所有处理和呈现代码与模板分开,这样表单就可以在任何模板上工作页面,您将表单的简码放入其中的内容.该操作只能以"#"定位到当前页面,也可以为空白,因为您正在挂接* all *请求以检查您的表单是否已提交,所以该操作可以在任何页面上进行.you wouldn't have to use `do_shortcode`, my point was that you could add it via a shortcode to a post/page's content, then all your processing and rendering code is separated from the template, that way the form could work on any page you place the form's shortcode within the content of. the action can just target the current page with a `#`, or be blank, since you're hooking *all* requests to check if your form was submitted, it will work from/to any page.
- 1
- 2012-12-23
- Milo
-
@Milo,你为我钉了这个.对我来说,"标题已发送"是问题.谢谢@Milo you nailed this for me. "headers already sent" was the problem for me. Thanks
- 0
- 2013-09-24
- henrywright
-
- 2012-12-22
将
get_header();
移至该代码的底部应该可以解决该问题.您的代码将在发送任何标头之前执行,并且重定向将起作用.// ... wp_redirect( get_permalink($pid) ); exit(); //insert taxonomies } get_header(); ?>
我假设您发布的页面下方的页面上还有更多代码?如果不是,我根本看不到需要
get_header()
.我所看到的使用Milo建议的使用钩子的唯一好处是,如果您选择了足够早的钩子,则可以避免一些开销.您可以节省几分之一秒的处理时间.
Moving
get_header();
to the bottom of that code should fix the problem. Your code will execute before any headers are sent and the redirect will work.// ... wp_redirect( get_permalink($pid) ); exit(); //insert taxonomies } get_header(); ?>
I assume there is more code on the page below what you posted? If not I don't see the need for
get_header()
at all.The only benefit I can see to using a hook as Milo suggests is that you might be able to avoid some overhead if you pick an early enough hook. You could shave a fraction of a second off of processing.
-
是的,这里有一些HTML,还有更多的wp函数get_sidebars()和get_footer()等.我对使用钩子一点都不熟悉,但我确实想看看一个例子.我已经在谷歌上搜索了,看到有人在谈论`add_action('wp_loaded','your_function')`,但是真的不确定如何使用它.任何例子表示赞赏,谢谢Yes there's some HTML, and some more wp functions get_sidebars(), and get_footer() etc. I'm not at all familiar with using hooks but would really like to see an example. I'm already googling and see people talking about `add_action('wp_loaded', 'your_function')` but really not sure how to use it. Any examples is appreciated thanks
- 0
- 2012-12-22
- Anagio
-
我会等一会儿,看看@Milo是否使用钩子发布了示例,因为这是他的答案.如果没有,我将编辑答案.I'll wait awhile and see if @Milo posts an example using a hook, since that is his answer. If not, I'll edit my answer.
- 0
- 2012-12-22
- s_ha_dum
-
感谢将get_header()移到表单处理代码下方,并进行了重定向.我想看看如何使用钩子.Thanks moving the get_header() below the form handling code and redirect worked. I would like to see how to use the hook though.
- 0
- 2012-12-22
- Anagio
-
@s_ha_dum,这个建议简而言之就是一块钻石.:)它解释了一切.我尝试了很多方法-所有`wp_loaded`,`template_redirect`事物,但是无法使事物起作用.非常感谢.@s_ha_dum that piece of suggestion is a piece of diamond in a nutshell. :) It explained everything. I tried a lots of ways - all the `wp_loaded`, `template_redirect` things, but could not make things work. Thanks a lot.
- 0
- 2015-04-27
- Mayeenul Islam
插入帖子后,我正在使用此重定向.它不起作用,它仅刷新表单所在的页面.我知道$pid正在获取帖子ID,这是什么问题?这是我处理表单提交的php代码的结尾.
这是完整代码的pastebin
使用更好的HTTP重定向,它的输出是,并将
找到here
一词链接到正确的新发布的帖子.