从WordPress API取消设置数据(wp-json)

时间:2015-09-10 20:39:01

标签: php json wordpress

我已经可以在从WordPress API返回的json中取消设置(从正常帖子中删除细节)。我实际上在此示例中使用了以下内容:https://css-tricks.com/using-the-wp-api-to-fetch-posts/

我遇到问题并且无法弄清楚,如何更改此设置以便从自定义帖子类型

中取消设置数据

思想?

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

3 个答案:

答案 0 :(得分: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 );
    }
}

将“Pages”替换为“MyCustomPostTypes”,将页面替换为“mycustomposttype”。请注意不要重命名也使用术语page

的内部WordPress代码

注意:最好将其添加为插件而不是更改JSON-WP-API插件

/**
 * 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 :(得分: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 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);

重要: 方法是:     add_filter(&#39; rest_prepare_post &#39;&#39; qod_remove_extra_data&#39;,12,3);

加:     add_filter(&#39; json_prepare_post &#39;&#39; qod删除extra_data&#39;,12,3); // WRONG

如果是自定义帖子类型:     add_filter(&#39; rest_prepare _ {$ post_type} &#39;&#39; qod_remove_extra_data&#39;,12,3);

示例:姓名发布类型=产品;      add_filter(&#39; rest_prepare_product &#39;&#39; qod_remove_extra_data&#39;,12,3);

使用此代码可以删除您想要JSON的字段。通过使用rest_prepare} _ {$ post_type决定你删除了每个post_type字段,因此只影响了你想要的post_type而不是全部。

答案 2 :(得分:1)

从自定义帖子类型中删除数据与从内置帖子类型中删除数据应该没有什么不同。您确认您的API通话实际上已退回您的CPT吗?首先,您应该查看从http://yourwebsite.com/wp-json/posts/types返回的值的值。假设您的CPT类型显示在那里,您应该能够查询该类型的项目,例如product,致电:http://yourwebsite.com/wp-json/posts?type=product

换句话说,您应更改过滤器的名称:您仍希望与json_prepare_post绑定。如果您想让过滤器对帖子类型敏感,并且只有删除某些字段(如果您有CPT),您可以执行以下操作:

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

中找到更多文档