2015-09-10 9 views
12

मैं वर्डप्रेस एपीआई से वापस जेसन में पहले ही अनसेट (सामान्य पोस्ट से विशिष्टताओं को हटा सकता हूं) कर सकता हूं। मैं वास्तव में उपयोग इस उदाहरण से नीचे निम्नलिखित: https://css-tricks.com/using-the-wp-api-to-fetch-posts/वर्डप्रेस एपीआई (wp-json) से डेटा अनसेट करें

क्या मैं साथ परेशानी हो रही हूँ और को समझ नहीं सकता, है कि यह कैसे तो यह एक कस्टम पोस्ट प्रकार

विचार से डेटा unsets बदलने के लिए?

function qod_remove_extra_data($data, $post, $context) { 
    // We only want to modify the 'view' context, for reading posts 
    if ($context !== 'view' || is_wp_error($data)) { 
    return $data; 
    } 

    // Here, we unset any data we don't want to see on the front end: 
    unset($data['author']); 
    unset($data['status']); 
    unset($data['featured_image']); 
    //etc etc 

    return $data; 
} 

add_filter('json_prepare_post', 'qod_remove_extra_data', 12, 3); 

कस्टम पोस्ट प्रकार उदाहरण फिल्टर:

function projectPost_remove_extra_data($data, $post, $context) { 

    if ($context !== 'view' || is_wp_error($data)) { 
    return $data; 
    } 

    // Here, we unset any data we don't want to see on the front end: 
    unset($data['author']); 



    return $data; 
} 

add_filter('json_prepare_project', 'projectPost_remove_extra_data', 12, 3); 
+0

आप किस एपीआई संस्करण का उपयोग कर रहे हैं? यह v2 के लिए बदल रहा है। कृपया देखें: https://github.com/WP-API/WP-API/issues/1195 – brianlmerritt

+0

@brainlmeritt मैं संस्करण 1 – RMH

उत्तर

3

WP-api v1.x के लिए, आप WP_JSON_CustomPostType का विस्तार करने की जरूरत है। वहाँ एक उदाहरण में पृष्ठों फ़ाइल (class-wp-json-pages.php)

<?php 
/** 
* Page post type handlers 
* 
* @package WordPress 
* @subpackage JSON API 
*/ 

/** 
* Page post type handlers 
* 
* This class serves as a small addition on top of the basic post handlers to 
* add small functionality on top of the existing API. 
* 
* In addition, this class serves as a sample implementation of building on top 
* of the existing APIs for custom post types. 
* 
* @package WordPress 
* @subpackage JSON API 
*/ 
class WP_JSON_Pages extends WP_JSON_CustomPostType { 
    /** 
    * Base route 
    * 
    * @var string 
    */ 
    protected $base = '/pages'; 

    /** 
    * Post type 
    * 
    * @var string 
    */ 
    protected $type = 'page'; 

    /** 
    * Register the page-related routes 
    * 
    * @param array $routes Existing routes 
    * @return array Modified routes 
    */ 
    public function register_routes($routes) { 
     $routes = parent::register_routes($routes); 
     $routes = parent::register_revision_routes($routes); 
     $routes = parent::register_comment_routes($routes); 

     // Add post-by-path routes 
     $routes[ $this->base . '/(?P<path>.+)'] = array(
      array(array($this, 'get_post_by_path'), WP_JSON_Server::READABLE), 
      array(array($this, 'edit_post_by_path'), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON), 
      array(array($this, 'delete_post_by_path'), WP_JSON_Server::DELETABLE), 
     ); 

     return $routes; 
    } 

    /** 
    * Retrieve a page by path name 
    * 
    * @param string $path 
    * @param string $context 
    * 
    * @return array|WP_Error 
    */ 
    public function get_post_by_path($path, $context = 'view') { 
     $post = get_page_by_path($path, ARRAY_A); 

     if (empty($post)) { 
      return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); 
     } 

     return $this->get_post($post['ID'], $context); 
    } 

    /** 
    * Edit a page by path name 
    * 
    * @param $path 
    * @param $data 
    * @param array $_headers 
    * 
    * @return true|WP_Error 
    */ 
    public function edit_post_by_path($path, $data, $_headers = array()) { 
     $post = get_page_by_path($path, ARRAY_A); 

     if (empty($post)) { 
      return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); 
     } 

     return $this->edit_post($post['ID'], $data, $_headers); 
    } 

    /** 
    * Delete a page by path name 
    * 
    * @param $path 
    * @param bool $force 
    * 
    * @return true|WP_Error 
    */ 
    public function delete_post_by_path($path, $force = false) { 
     $post = get_page_by_path($path, ARRAY_A); 

     if (empty($post)) { 
      return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); 
     } 

     return $this->delete_post($post['ID'], $force); 
    } 

    /** 
    * Prepare post data 
    * 
    * @param array $post The unprepared post data 
    * @param string $context The context for the prepared post. (view|view-revision|edit|embed|single-parent) 
    * @return array The prepared post data 
    */ 
    protected function prepare_post($post, $context = 'view') { 
     $_post = parent::prepare_post($post, $context); 

     // Override entity meta keys with the correct links 
     $_post['meta']['links']['self'] = json_url($this->base . '/' . get_page_uri($post['ID'])); 

     if (! empty($post['post_parent'])) { 
      $_post['meta']['links']['up'] = json_url($this->base . '/' . get_page_uri((int) $post['post_parent'])); 
     } 

     return apply_filters('json_prepare_page', $_post, $post, $context); 
    } 
} 

"पेज" बदलें "mycustomposttype" के साथ "MyCustomPostTypes" और पेज के साथ है। यह होना चाहिए नहीं JSON-WP-एपीआई प्लगइन

/** 
* Plugin Name: MyCustom JSON App API 
* Description: MyCustomPost handler for the JSON API 
* Dependency: This plugin requires JSON-WP-API Plugin!!!! 
* Author: 
* Author URI: 
* Version: 
* Plugin URI: 
*/ 
1

शायद सबसे अच्छा एक प्लगइन के रूप में जोड़ना बजाय बदलने के लिए: बस ऐसे भी शब्द का उपयोग करता page

नोट आंतरिक वर्डप्रेस कोड नाम बदलने के लिए सावधान नहीं हो अंतर्निहित पोस्ट प्रकारों की तुलना में कस्टम पोस्ट प्रकारों से डेटा को निकालने के लिए अलग-अलग। क्या आपने पुष्टि की है कि आपका एपीआई कॉल वास्तव में आपके सीपीटी वापस कर रहा है? सबसे पहले, आपको http://yourwebsite.com/wp-json/posts/types से लौटाए गए मूल्य के मूल्य को देखना चाहिए। यह मानते हुए कि आपका सीपीटी प्रकार वहां दिखाई देता है, आपको उस प्रकार की वस्तुओं के लिए पूछताछ करने में सक्षम होना चाहिए, उदा। product, कॉल करके: http://yourwebsite.com/wp-json/posts?type=product

दूसरे शब्दों में, आपको फ़िल्टर का नाम बदलना चाहिए: आप अभी भी json_prepare_post में टाई करना चाहते हैं।

function my_remove_extra_product_data($data, $post, $context) { 
    // make sure you've got the right custom post type 
    if ('product' !== $data[ 'type' ]) { 
     return $data; 
    } 
    // now proceed as you saw in the other examples 
    if ($context !== 'view' || is_wp_error($data)) { 
     return $data; 
    } 
    // unset unwanted fields 
    unset($data[ 'author' ]); 

    // finally, return the filtered data 
    return $data; 
} 

// make sure you use the SAME filter hook as for regular posts 
add_filter('json_prepare_post', 'my_remove_extra_product_data', 12, 3); 

आप WP API Getting Started Guide में अधिक प्रलेखन पा सकते हैं: आप अपने फ़िल्टर प्रकार पोस्ट करने के लिए संवेदनशील बनाने के लिए और केवल कुछ फ़ील्ड को दूर करता है, तो आप एक सीपीटी आप की तरह कुछ कर सकता है चाहते हैं।

+0

का उपयोग कर रहा हूं मैंने आपके द्वारा सूचीबद्ध किए गए कार्यों के लिए अपना कामकाजी उदाहरण अपडेट किया है, और यह किसी को भी परेशान करने में सक्षम नहीं था डेटा का एपीआई काम कर रहा है, और मैं वास्तव में शीर्षक, एसीएफ क्षेत्र और सामग्री क्षेत्र प्राप्त करने के लिए इसे एजेक्स कॉल के साथ मारा। – RMH

+0

क्या आप अपनी साइट के लिए एक लिंक प्रदान कर सकते हैं? – morphatic

+0

आह, ठीक है, यह अच्छा लगता है। फ़ंक्शन के अंदर, पहली पंक्ति के रूप में, निम्न जोड़ें: 'echo'

' . print_r($data) . '
'; बाहर निकलें; 'और इसे फिर से चलाएं। यह '$ डेटा' चर के एक var डंप करना चाहिए। यह बहुत संभव है कि या तो हम उन चरों को अनसेट करने का प्रयास कर रहे हैं जो मौजूद नहीं हैं, या हम ऑब्जेक्ट सदस्यों को सरणी सूचकांक के रूप में एक्सेस करने का प्रयास कर रहे हैं। – morphatic

2

यदि संभव हो, केवल इंटरनेट में दिखाया गया उदाहरण है:

qod_remove_extra_data function ($ data, $ post, $ context) { 
    // We only want to modify the 'view' context, for reading posts 
    if ($ context! == 'view' || is_wp_error ($ data)) { 
     return $ data; 
    } 
    // Here, we unset any data we do not want to see on the front end: 
    unset ($data ['author']); 
    unset ($data ['status']); 
    // Continue unsetting whatever other fields you want return $ data; 
} 
add_filter ('json_prepare_post' 'qod remove extra_data', 12, 3); 

और सही है: 'qod_remove_extra_data add_filter (' rest_prepare_post ':

qod_remove_extra_data function ($ data, $ post, $ context) { 
    // We only want to modify the 'view' context, for reading posts 
    if ($ context! == 'view' || is_wp_error ($ data)) { 
     unset ($data->data ['excerpt']); //Example 
     unset ($data->data ['content']); //Example 
     unset ($data->data ['name field to remove']) 
     //or 
     unset ($data->data ['name field to remove'] ['name subfield if you only want to delete the sub-field of field' ]) 
     return $data; 
    } 
} 
add_filter ('rest_prepare_post' 'qod_remove_extra_data', 12, 3); 

महत्वपूर्ण: है ', 12, 3);

नहीं

: add_filter ('json_prepare_post' 'qod निकालें extra_data', 12, 3); // गलत

तो है कस्टम पोस्ट प्रकार: add_filter ('rest_prepare _ {$ post_type}' 'qod_remove_extra_data', 12, 3);

उदाहरण: नाम पोस्ट प्रकार = उत्पाद; add_filter ('rest_prepare_product' 'qod_remove_extra_data', 12, 3);

इस कोड के साथ आप जो JSON चाहते हैं उसे हटा सकते हैं। Rest_prepare} _ {$ post_type का उपयोग करके आप तय करते हैं कि आपने प्रत्येक पोस्ट_टाइप फ़ील्ड को हटा दिया है, इस प्रकार केवल उस पोस्ट_ टाइप को प्रभावित किया है जो आप चाहते हैं और सभी नहीं।

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