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?

2 Answers
2

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

References

I found pretty much everything I needed on the WP API site.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *