查看页面的wp api代码(这是自定义帖子类型)。
您可以修改代码,将页面更改为调用的任何帖子类型(只需小心搜索和替换所有“页面”,因为其中一些是wordpress函数调用或过滤器)
<?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 );
}
}
将您的自定义代码置于编辑或筛选等状态,然后就可以开始了!
ps-别忘了把它变成一个合适的插件!您可以添加为新插件,并通过以下方式更好地管理它:
<?php
/**
* Plugin Name: My JSON App API
* Description: My Route and Endpoint handler for the JSON API
* Dependency: This plugin requires JSON BasicKey Authentication Plugin!!!!
* Author: Blah Blah Blah, plus much original code from the WordPress API Team
* Author URI: https://www.example.com
* Version: 1.2
* Plugin URI: https://www.example.com
*/
if ( ! defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly
if (!defined("MY_JSON_API_VERSION")) {
define ("MY_JSON_API_VERSION", "1.2") ;
}
function my_json_api_init() {
global $my_json_api_mobile_users;
$my_json_api_mobile_users = new my_JSON_API_MobileUsers();
add_filter( \'json_endpoints\', array( $my_json_api_mobile_users, \'register_routes\' ) );
}
add_action( \'wp_json_server_before_serve\', \'my_json_api_init\' );