2010-02-05 15 views
9

ठीक है, मुझे यह कोड मिला है जिसका उपयोग मैं अपने आवेदन के लिए समाचार थूकने के लिए कर रहा हूं। यह आज तक काम कर रहा था। मैंने इसे सिंपलर बनाने के लिए निम्न कोड में सभी तर्कों को काट दिया है। लेकिन इसे "काम करना चाहिए" क्या कोई इस कोड को ठीक करने में मदद कर सकता है जहां यह काम करता है, और सही हो गया है? मुझे पता है कि यह एक साथ हैक किया गया है, लेकिन ऐसा लगता है कि आज तक कोई समस्या नहीं है। मैंने कुछ भी अपडेट नहीं किया है, यह नहीं पता कि सौदा क्या है।वर्डप्रेस प्लगइन में wp_rewrite



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

मुझे मूल रूप से केवल एक यूआरएल से इनपुट लेने और कुछ JSON डेटा आउटपुट करने में सक्षम होना चाहिए। – Brad

उत्तर

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')); 

मैं महत्वपूर्ण भागों टिप्पणी की है, लेकिन क्या मैंने किया था अपने हुक स्थानांतरित करने के लिए मूल रूप से था add_action के बजाय use_filter पर। मैंने फ़िल्टर को इस क्रम में भी स्थानांतरित कर दिया कि वे वास्तव में वर्डप्रेस में उपयोग किए जाते हैं। उस समय काम करने के लिए समझा।

अंत में, सुनिश्चित करें कि आपके परमालिंक सुंदर यूआरएल का उपयोग करने के लिए सेट हैं। मुझे एक मुद्दा था जहां मेरा डिफ़ॉल्ट रूप से सेट किया गया था, जिससे वर्डप्रेस किसी भी पुनर्लेखन शर्तों को अनदेखा कर देता है जिसे अन्यथा इसे पार्स करने की आवश्यकता होती है। कुछ सुंदर यूआरएल में बदलें और आपकी स्थितियां ताज़ा हो जाएंगी।

मुझे बताएं कि यह आपके लिए काम करता है या नहीं। उम्मीद है की वो मदद करदे।

धन्यवाद, जो

+0

इसके लिए धन्यवाद, मैं अब इसका परीक्षण कर रहा हूं! एक प्रश्न, यह देखने के लिए कि क्या यह सक्रिय है, $ wp_rewrite नियमों का परीक्षण करने का एक उचित तरीका क्या होगा? – Brad

+0

आमतौर पर यह आपके यूआरएल w/o को फिर से लिखने के लिए पर्याप्त है और इसके साथ (/index.php?MyPlugin=foo और/MyPlugin/foo) और थोड़ा परीक्षण और त्रुटि का उपयोग करें। यदि नहीं, तो पुनर्लेखन कोड wp-include/rewrite.php में है। हालांकि, उस फ़ाइल में कोड के साथ डीबग करना बहुत आसान और न ही आसान है। –

+0

मुझे पता है कि यह पुराना है, लेकिन मुझे आश्चर्य है कि क्या इस दृष्टिकोण को अब flush_rewrite_rules() के साथ अपडेट किया जा सकता है? – helgatheviking

संबंधित मुद्दे