Is there a WordPress REST API for Multisite?
I know this is fully functional for normal WordPress, but does the API also allow usage of Multisite?
I'm using the REST API to pull data about one multisite installation and feed it to sites in another multisite installation.
Here's some of the code in use:
class WPSE205354_Demo {
function __construct() {
add_filter( 'json_endpoints', array( $this, 'register_routes' ) );
}
/**
* Register the additional API routes
* @param array $routes The routes from the WP REST API
* @return array The filtered array of API routes
*/
public function register_routes( array $routes ) {
$routes['/sites'] = array(
array( array( $this, 'get_sites' ), WP_JSON_Server::READABLE ),
);
return $routes;
}
/**
* Get the list of public sites
* @return array The list of public sites
*/
function get_sites() {
$args = array(
'public' => 1, // I only want the sites marked Public
'archived' => 0,
'mature' => 0,
'spam' => 0,
'deleted' => 0,
);
$sites = wp_get_sites( $args );
return $sites;
}
}
I've network-activated the plugin, and also the WP JSON API plugin (I'm using v. 1.2.3).
To see what that returns, you would go to http://example.com/wp-json/sites
(spoiler alert: it's the list of public sites in your WordPress Multisite network).
I found pretty much everything I needed on the WP API site.
wp_get_sites()
are not cached, so you might want to do that in your script.
6 years later but this might still be handy for those with the same issue - like me till very very recent.
Through this thread I landed on the right search in Google and found https://github.com/WP-API/multisite which instantly made all the REST API functionality of the WooCommerce REST API work for me (so I assume the regular WordPress REST API will work then as well). Yeah!!!
It's code from 2013 but it works and is by the hand of Ryan McCue who appears to be one of the co-leads of the original WordPress REST API back in 2012. That probably explains why it didn't need to be updated all those years.
So hopefully this multisite api support will make it into the WordPress core one day so it's tested with every new release as well. #fingerscrossed