I have been looking for a way to access multisite data trough WP API, but it seems very hard to find. One and only the best reference i get is from git the_glue but need some update since it not updated.
Below is my version or u can get form my git
<?php
/*
* Plugin Name: WordPress REST API Multisite
* Plugin URI:
* Description:
* Version: 1.0
* Author: Ghazali Tajuddin
* Author URI: http://www.ghazalitajuddin.com
* License: MIT
* */
class WPMUrestAPI{
function __construct() {
add_action( 'rest_api_init', array( $this, 'call2' ));
}
public function call2() {
// Here we are registering our route for a collection of products and creation of products.
register_rest_route( 'all/sites', '/list',
array(
// By using this constant we ensure that when the WP_REST_Server changes, our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => array($this,'get_sites123'),
)
);
}
public function prefix_get_endpoint_phrase() {
// rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
//return rest_ensure_response( 'Hello World, this is the WordPress REST API' );
return 'Hello World, this is the WordPress REST API';
}
public function get_sites123() {
if ( function_exists( 'get_sites' ) ) {
$sites = get_sites(
[
'public' => 1,
//'number' => 500,
'orderby' => 'registered',
'order' => 'DESC',
]
);
$sites_details = array();
foreach ($sites as $site) {
$sites_details[] = $site->blog_id;
}
return $sites_details;
}
}
}
$power = new WPMUrestAPI();
Other reference
- https://github.com/epfl-lts2/json-rest-api-multisites
- https://github.com/remkade/multisite-json-api
- https://wordpress.org/support/topic/wp_get_sites-is-now-get_sites/