php – 从WordPress API中取消数据(wp-json)

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 从WordPress API中取消数据(wp-json)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以从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,$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',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 ),'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' => @L_304_12@ ) );
        }

        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( 'status' => 404 ) );
        }

        return $this->edit_post( $post['ID'],$_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( '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,$context );
    }
}

将“页面”替换为“MyCustomPostTypes”,页面替换为“mycustomposttype”.只要小心不要重命名也使用页面的内部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: 
 */

脚本宝典总结

以上是脚本宝典为你收集整理的php – 从WordPress API中取消数据(wp-json)全部内容,希望文章能够帮你解决php – 从WordPress API中取消数据(wp-json)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。