php – wp_rewrite在WordPress插件

前端之家收集整理的这篇文章主要介绍了php – wp_rewrite在WordPress插件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@ 好的,我有这个我一直在使用的代码,向我的应用程序吐出消息.直到今天才起作用.我已经删除了以下代码中的所有逻辑,使其成为simpiler.但是它应该“工作”有人可以帮助我修复这个代码到它的工作,并做好了吗?我知道它一起被黑客攻击,直到今天才似乎没有任何问题.我没有更新任何东西,不知道交易是什么.
  1. Plugin Name: MyPlugin Example
  2. Version: 1.0.1
  3.  
  4.  
  5. If ( ! class_exists("MyPlugin") )
  6. {
  7. class MyPlugin
  8. {
  9. var $db_version = "1.0"; //not used yet
  10.  
  11. function init()
  12. {
  13. //Nothing as of now.
  14. }
  15. function activate()
  16. {
  17. global $wp_rewrite;
  18. $this->flush_rewrite_rules();
  19. }
  20.  
  21. function pushoutput( $id )
  22. {
  23. $output->out =' The output worked!';
  24. $this->output( $output );
  25.  
  26. }
  27. function output( $output )
  28. {
  29. ob_start();
  30. ob_end_clean();
  31. header( 'Cache-Control: no-cache,must-revalidate' );
  32. header( 'Expires: Mon,26 Jul 1997 05:00:00 GMT' );
  33. header( 'Content-type: application/json' );
  34.  
  35. echo json_encode( $output );
  36. //Must encode this...
  37. }
  38.  
  39. function flush_rewrite_rules()
  40. {
  41. global $wp_rewrite;
  42. $wp_rewrite->flush_rules();
  43. }
  44.  
  45. function createRewriteRules( $rewrite )
  46. {
  47. global $wp_rewrite;
  48. $new_rules = array( 'MyPlugin/(.+)' => 'index.PHP?MyPlugin=' . $wp_rewrite->preg_index(1) );
  49. if ( ! is_array($wp_rewrite->rules) )
  50. {
  51. $wp_rewrite->rules = array();
  52. }
  53. $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  54. return $wp_rewrite;
  55. }
  56.  
  57.  
  58. function add_query_vars( $qvars )
  59. {
  60. $qvars[] = 'MyPlugin';
  61. return $qvars;
  62. }
  63. function template_redirect_intercept()
  64. {
  65. global $wp_query;
  66. if ( $wp_query->get('MyPlugin') )
  67. {
  68. $id = $wp_query->query_vars['MyPlugin'];
  69. $this->pushoutput( $id );
  70.  
  71.  
  72. exit;
  73. }
  74. }
  75. }
  76. }
  77. If ( class_exists("MyPlugin") )
  78. {
  79. $MyPluginCode = new MyPlugin();
  80. }
  81. If ( isset($MyPluginCode) )
  82. {
  83. register_activation_hook( __file__,array($MyPluginCode,'activate') );
  84. add_action( 'admin-init',array(&$MyPluginCode,'flush_rewrite_rules') );
  85. //add_action( 'init','init') );
  86. add_action( 'generate_rewrite_rules','createRewriteRules') );
  87.  
  88. add_action( 'template_redirect','template_redirect_intercept') );
  89. // add_filter( 'query_vars','add_query_vars') );
  90. }
我在这个过程中改变了一点代码,但这对我有用:
  1. <?PHP
  2.  
  3. /**
  4. * Plugin Name: MyPlugin Example
  5. * Version: 1.0.1
  6. **/
  7. class MyPlugin {
  8.  
  9. function activate() {
  10. global $wp_rewrite;
  11. $this->flush_rewrite_rules();
  12. }
  13.  
  14. // Took out the $wp_rewrite->rules replacement so the rewrite rules filter could handle this.
  15. function create_rewrite_rules($rules) {
  16. global $wp_rewrite;
  17. $newRule = array('MyPlugin/(.+)' => 'index.PHP?MyPlugin='.$wp_rewrite->preg_index(1));
  18. $newRules = $newRule + $rules;
  19. return $newRules;
  20. }
  21.  
  22. function add_query_vars($qvars) {
  23. $qvars[] = 'MyPlugin';
  24. return $qvars;
  25. }
  26.  
  27. function flush_rewrite_rules() {
  28. global $wp_rewrite;
  29. $wp_rewrite->flush_rules();
  30. }
  31.  
  32. function template_redirect_intercept() {
  33. global $wp_query;
  34. if ($wp_query->get('MyPlugin')) {
  35. $this->pushoutput($wp_query->get('MyPlugin'));
  36. exit;
  37. }
  38. }
  39.  
  40. function pushoutput($message) {
  41. $this->output($message);
  42. }
  43.  
  44. function output( $output ) {
  45. header( 'Cache-Control: no-cache,must-revalidate' );
  46. header( 'Expires: Mon,26 Jul 1997 05:00:00 GMT' );
  47.  
  48. // Commented to display in browser.
  49. // header( 'Content-type: application/json' );
  50.  
  51. echo json_encode( $output );
  52. }
  53. }
  54.  
  55. $MyPluginCode = new MyPlugin();
  56. register_activation_hook( __file__,'activate') );
  57.  
  58. // Using a filter instead of an action to create the rewrite rules.
  59. // Write rules -> Add query vars -> Recalculate rewrite rules
  60. add_filter('rewrite_rules_array','create_rewrite_rules'));
  61. add_filter('query_vars','add_query_vars'));
  62.  
  63. // Recalculates rewrite rules during admin init to save resourcees.
  64. // Could probably run it once as long as it isn't going to change or check the
  65. // $wp_rewrite rules to see if it's active.
  66. add_filter('admin_init','flush_rewrite_rules'));
  67. add_action( 'template_redirect','template_redirect_intercept') );

我已经评论过重要的部分,但是我所做的是基本上将钩子移到use_filter而不是add_action.我也将过滤器移动到wordpress中实际使用的顺序.看来当时要做的事情

最后,确保您的永久链接设置为使用漂亮的URL.我有一个问题,我的设置为默认,这使wordpress忽略任何重写条件,否则需要解析.更改为一些漂亮的URL,您的条件将刷新.

让我知道,如果这适用于你.希望有帮助.

谢谢,乔

猜你在找的PHP相关文章