3

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?

  • fully functional? and I thought that at best the REST API will arrive only at 4.5.... – Mark Kaplun Oct 14 '15 at 15:41
4

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.

  • 1
    Be aware that calls to wp_get_sites() are not cached, so you might want to do that in your script. – fuxia Oct 14 '15 at 14:14
  • True. My network isn't all that large -- about 10 sites to date -- but it might be wise to add some caching to my (very basic) code. – Pat J Oct 14 '15 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.