2012-08-26 16 views
6

मेरे अपने विवेक के लिए मैं एक AJAX API ऐसा ही कुछ लग रहा है के लिए एक मार्ग बनाने के लिए कोशिश कर रहा हूँ। क्या वर्डप्रेस मुझे इसे लागू करने के लिए एक हुक देता है? एक अच्छी जगह कहां है?मैं वर्डप्रेस में 'रूट' कैसे बना सकता हूं?</p> <pre><code>/api/<action> </code></pre> <p>मैं इस मार्ग को संभालने और <code>do_action</code> के साथ उचित कार्रवाई करने के लिए सौंपने के लिए वर्डप्रेस चाहते हैं:

+0

'wordpress.stackexchange.com' पर इस पर एक समान सवाल है। http://wordpress.stackexchange.com/questions/26388/how-to-create-custom-url-routes – ckpepper02

उत्तर

1

ऐसा लगता है कि आप वर्डप्रेस json-api प्लगइन की तलाश में हैं, जो मैंने उपयोग किए गए अच्छी तरह से निर्मित प्लगइन में से एक है, यह भी बहुत आसानी से विस्तार योग्य है। शुभकामनाएं।

add_action('init', 'theme_functionality_urls'); 

function theme_functionality_urls() { 

    /* Order section by fb likes */ 
    add_rewrite_rule(
    '^tus-fotos/mas-votadas/page/(\d)?', 
    'index.php?post_type=usercontent&orderby=fb_likes&paged=$matches[1]', 
    'top' 
); 
    add_rewrite_rule(
    '^tus-fotos/mas-votadas?', 
    'index.php?post_type=usercontent&orderby=fb_likes', 
    'top' 
); 

} 

यह /tus-fotos/mas-votadas और /tus-fotos/mas-votadas/page/{number} बनाता है, जो किसी कस्टम एक है, जो मैं pre_get_posts फिल्टर में संभाल के लिए orderby क्वेरी वर परिवर्तन:

10

आप का उपयोग करने के add_rewrite_rule

कुछ की तरह है।

query_vars फ़िल्टर का उपयोग करके नए चर को भी जोड़ा जा सकता है और इसे पुनः लिखने के नियम में जोड़ दिया जा सकता है।

add_filter('query_vars', 'custom_query_vars'); 
add_action('init', 'theme_functionality_urls'); 

function custom_query_vars($vars){ 
    $vars[] = 'api_action'; 
    return $vars; 
} 

function theme_functionality_urls() { 

    add_rewrite_rule(
    '^api/(\w)?', 
    'index.php?api_action=$matches[1]', 
    'top' 
); 

} 

फिर, कस्टम अनुरोध को पूरा:

add_action('parse_request', 'custom_requests'); 
function custom_requests ($wp) { 

    $valid_actions = array('action1', 'action2'); 

    if(
    !empty($wp->query_vars['api_action']) && 
    in_array($wp->query_vars['api_action'], $valid_actions) 
) { 

    // do something here 

    } 

} 

बस /wp-admin/options-permalink.php पर जाकर या केवल जब की जरूरत है, यह एक छोटी सी प्रक्रिया नहीं है के बाद से flush_rewrite_rulesफोन करके फिर से लिखने के नियम फ्लश करने के लिए याद है।

+0

यहां WordPress द्वारा वास्तविक और पूर्ण समाधान है: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule –

+0

क्या वह वही यूआरएल मेरी प्रतिक्रिया के पहले सेंसेंट पर उपयोग नहीं किया जाता है? – davidmh

+0

क्षमा करें आदमी, लेकिन मैंने साझा किया कि मेरे लिए क्या काम किया है ताकि यह भविष्य में किसी अन्य की मदद कर सके :) –