开发者

How to achieve Post-Redirect-Get in a Wordpress plugin's admin menu page?

I am writing 开发者_如何学Ca Wordpress plugin, which adds an admin menu page. In the page is a form. When the form is submitted, the plugin writes to the database. But then I encounter a problem: whenever the user reloads the page, he/she is asked whether to send the POSTDATA again. If the user clicks yes, the plugin writes to the database again.

After some searching, I found a solution, the "Post-Redirect-Get" pattern. Then, later, I found that it's hard to implement this pattern into a Wordpress plugin.

  1. The plugin itself cannot send an HTTP 301/302 because there are some contents already outputted by the Wordpress core.

  2. It is possible to insert a meta tag (in order to issue a redirect) in the HTML head. But according to W3C, meta redirect is deprecated, so I think it's better not to use it.

  3. Use JavaScript's window.location. But what if JavaScript is disabled in the user's browser?

Is there any other way to achieve redirection?


You could try doing your plugin's processing in the admin_init hook, which, I believe, is run before any content is output.


To save a LOT of work. Just use the WordPress settings api.

Codex article -> here

More helpful article by Otto -> here

Using the WordPress settings api will take care of the "Post-Redirect-Get" issue you are describing.


  1. check if table exist in the data base or insert in sql if not exists for not action if the table exists in the data base.

  2. option is set_option where plugin is activated.

  3. use register_activation_hook wordpress function for include options plugin activate

sorry for my english, is very bad, but I think you can understand

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜