4

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?

1
  • fully functional? and I thought that at best the REST API will arrive only at 4.5.... Commented Oct 14, 2015 at 15:41

2 Answers 2

6

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.

2
  • 1
    Be aware that calls to wp_get_sites() are not cached, so you might want to do that in your script.
    – fuxia
    Commented Oct 14, 2015 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
    Commented Oct 14, 2015 at 14:18
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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