2010-02-05 77 views
9

好吧,我已經得到了我用來向我的應用程序吐出新聞的代碼。它一直工作到今天。我已經刪除了以下代碼中的所有邏輯,以使它更簡單。但它應該「工作」有人可以幫我修復這個代碼到它的工作地點,而且做得對嗎?我知道它被一起黑了,但直到今天似乎沒有任何問題。我沒有更新任何東西,不知道交易是什麼。wp_rewrite在一個WordPress插件



Plugin Name: MyPlugin Example 
Version:  1.0.1 


If (! class_exists("MyPlugin")) 
{ 
    class MyPlugin 
    { 
     var $db_version = "1.0"; //not used yet 

     function init() 
     { 
    //Nothing as of now. 
     } 
     function activate() 
     { 
      global $wp_rewrite; 
      $this->flush_rewrite_rules(); 
     } 

     function pushoutput($id) 
     { 
      $output->out =' The output worked!'; 
      $this->output($output); 

     } 
     function output($output) 
     { 
      ob_start(); 
      ob_end_clean(); 
      header('Cache-Control: no-cache, must-revalidate'); 
      header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
      header('Content-type: application/json'); 

      echo json_encode($output); 
      //Must encode this... 
     } 

     function flush_rewrite_rules() 
     { 
      global $wp_rewrite; 
      $wp_rewrite->flush_rules(); 
     } 

     function createRewriteRules($rewrite) 
     { 
      global $wp_rewrite; 
      $new_rules = array('MyPlugin/(.+)' => 'index.php?MyPlugin=' . $wp_rewrite->preg_index(1)); 
      if (! is_array($wp_rewrite->rules)) 
      { 
       $wp_rewrite->rules = array(); 
      } 
      $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; 
      return $wp_rewrite; 
     } 


     function add_query_vars($qvars) 
     { 
      $qvars[] = 'MyPlugin'; 
      return $qvars; 
     } 
     function template_redirect_intercept() 
     { 
      global $wp_query; 
      if ($wp_query->get('MyPlugin')) 
      { 
       $id = $wp_query->query_vars['MyPlugin']; 
       $this->pushoutput($id); 


       exit; 
      } 
     } 
    } 
} 
If (class_exists("MyPlugin")) 
{ 
    $MyPluginCode = new MyPlugin(); 
} 
If (isset($MyPluginCode)) 
{ 
    register_activation_hook(__file__, array($MyPluginCode, 'activate')); 
    add_action('admin-init', array(&$MyPluginCode, 'flush_rewrite_rules')); 
    //add_action('init', array(&$MyPluginCode, 'init')); 
    add_action('generate_rewrite_rules', array(&$MyPluginCode, 'createRewriteRules')); 

    add_action('template_redirect', array(&$MyPluginCode, 'template_redirect_intercept')); 
    // add_filter('query_vars', array(&$MyPluginCode, 'add_query_vars')); 
} 

+0

我基本上只需要能夠從一個URL的輸入和輸出一些JSON數據。 – Brad 2010-02-05 22:16:05

回答

20

我改變的過程中你的代碼一點點,但是這個工作對我來說:

<?php 

/** 
* Plugin Name: MyPlugin Example 
* Version:  1.0.1 
**/ 
class MyPlugin { 

    function activate() { 
     global $wp_rewrite; 
     $this->flush_rewrite_rules(); 
    } 

    // Took out the $wp_rewrite->rules replacement so the rewrite rules filter could handle this. 
    function create_rewrite_rules($rules) { 
     global $wp_rewrite; 
     $newRule = array('MyPlugin/(.+)' => 'index.php?MyPlugin='.$wp_rewrite->preg_index(1)); 
     $newRules = $newRule + $rules; 
     return $newRules; 
    } 

    function add_query_vars($qvars) { 
     $qvars[] = 'MyPlugin'; 
     return $qvars; 
    } 

    function flush_rewrite_rules() { 
     global $wp_rewrite; 
     $wp_rewrite->flush_rules(); 
    } 

    function template_redirect_intercept() { 
     global $wp_query; 
     if ($wp_query->get('MyPlugin')) { 
      $this->pushoutput($wp_query->get('MyPlugin')); 
      exit; 
     } 
    } 

    function pushoutput($message) { 
     $this->output($message); 
    } 

    function output($output) { 
     header('Cache-Control: no-cache, must-revalidate'); 
     header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 

     // Commented to display in browser. 
     // header('Content-type: application/json'); 

     echo json_encode($output); 
    } 
} 

$MyPluginCode = new MyPlugin(); 
register_activation_hook(__file__, array($MyPluginCode, 'activate')); 

// Using a filter instead of an action to create the rewrite rules. 
// Write rules -> Add query vars -> Recalculate rewrite rules 
add_filter('rewrite_rules_array', array($MyPluginCode, 'create_rewrite_rules')); 
add_filter('query_vars',array($MyPluginCode, 'add_query_vars')); 

// Recalculates rewrite rules during admin init to save resourcees. 
// Could probably run it once as long as it isn't going to change or check the 
// $wp_rewrite rules to see if it's active. 
add_filter('admin_init', array($MyPluginCode, 'flush_rewrite_rules')); 
add_action('template_redirect', array($MyPluginCode, 'template_redirect_intercept')); 

我評價的重要組成部分,但我所做的基本上是將您的掛鉤到use_filter而不是add_action。我還將這些過濾器按照它們在Wordpress中實際使用的順序移動。看起來當時要做的事情。

最後,確保您的固定鏈接設置爲使用漂亮的URL。我有一個問題,我的設置爲默認,這使得WordPress的忽略任何重寫條件,否則需要解析。切換到一些漂亮的網址,你的條件會刷新。

讓我知道,如果這對你有用。希望有所幫助。

謝謝,喬

+0

謝謝你,我正在測試它!有一個問題,測試$ wp_rewrite規則以確定它是否處於活動狀態的正確方法是什麼? – Brad 2010-03-22 07:03:32

+0

通常只需調用不帶重寫的URL並使用它(/index.php?MyPlugin=foo和/ MyPlugin/foo)就可以使用一點點試驗和錯誤。如果不是,則重寫代碼位於wp-includes/rewrite.php中。不過,使用該文件中的代碼進行調試並不容易,也不容易。 – 2010-03-22 07:21:33

+0

我知道這是舊的,但我想知道如果現在可以用flush_rewrite_rules()現在更新此方法? – helgatheviking 2012-01-08 16:20:39