Format of REST API URL for state.source.api

With the package wp-source we have to set a URL for the REST API server that is going to provide the data. This URL is set as the value of state.source.api

module.exports = {
  packages: [
    "@frontity/mars-theme",
    "@frontity/tiny-router",
    {
      name: "@frontity/wp-source",
      state: {
        source: {
          api: "https://site.com/wp-json",
        },
      },
    },
  ],
};

The format of this URL may vary depending on the type of Wordpress installation you want to use as a source of data.

For a self-hosted WordPress site (i.e. local or web based) it would be something like:


http://your-wp-domain.com/wp-json

For a wordpress.com site it would be something like:


https://public-api.wordpress.com/wp/v2/sites/your-wp-site.wordpress.com

In the case of self-hosted WordPress site, most of the sites recommend using the following format

http://yoursite.com/wp-json/wp/v2

As I understand this shows you only the part of the data that is under the namespace /wp/v2

For example If we go to https://test.frontity.org/wp-json/ we can see

Captura de pantalla 2020-07-01 a las 18.31.44

But if we go to https://test.frontity.org/wp-json/wp/v2 we only see the part corresponding to /wp/v2

So, I understand the format required is just https://test.frontity.org/wp-json/ so Frontity can also acesss to other namespaces like:

  • contact-form-7/v1 ā†’ Contact Form plugin data
  • yoast/v1 & yoast/v1/myyoast ā†’ Yoast plugin data
  • acf/v3 ā†’ Advanced Custom Fields plugin data
  • and othersā€¦

But what happens to the sites hosted in wordpress.com where api.source.data is using https://public-api.wordpress.com/wp/v2/sites/your-wp-site.wordpress.com ?

For example: https://public-api.wordpress.com/wp/v2/sites/juanmaguitarblog.wordpress.com

Isnā€™t this showing just the namespace /wp/v2?
I understand the free wordpress.com sites doesnā€™t allow you to install plugins but paid ones do.
What happen to these wordpress sites that have plugins installed?
Is there a different URL to be set in these cases?

And regarding self-hosted sitesā€¦
Couldnā€™t be Frontity smart enough to accepts either http://yoursite.com/wp-json/wp/v2 and http://yoursite.com/wp-json/ and take only the part that works? Is it doing this right now?

Itā€™s really not clear to me either and I cannot find this info online.
https://public-api.wordpress.com/wp/v2/ lists a number of routes that are fixed and I cannot see a way to access data from e.g. acf plugin if you pay for the premium hosting on wordpress.com and install the Advanced Custom Fields Plugin

@David do you have some idea here? Is is possible to access data from WP plugins in a namespace different than wp/v2 for sites hosted on wordpress.com

If I understand you correctly you are suggesting that frontity accepts both formats as a parameter to state.source.api, correct? I think this might work, because the site content is always fetched from the /wp/v2 endpoint, and we are actually always attaching the /wp/v2 here if he site is wp.com. But if the If the user wants to fetch custom data from a different plugin they have to do sth like:

fetch(`/plugin-name/version/endpoint`);

So, in that case, we donā€™t need the wp/v2.

The only thing that maybe conflicts with this is if we have some plans for wp-source v2.0 that would have a built-in way of fetching data from plugins that support the WP REST APIā€¦ @luisherranz Do you have any plans of this sort ?

1 Like

From my understanding, the weird WordPress.com REST API URL (https://public-api.wordpress.com/wp/v2/sites/) is only used for the /wp/v2 endpoint, which is the only thing you have in the free tier of WordPress.com anyway.

Once you want to install plugins, you need to upgrade to the Business plan. You also get a custom domain.

In that plan, you should be able to use https://mydomain.com/wp-json to access the REST API, just like any other self-hosted WordPress site.

I believe that is how it works, but I havenā€™t confirmed it yet. So itā€™d be awesome to confirm it 100% :slight_smile:

@juanma maybe you could upgrade a site to the Business plan and explore it a bit more.

Yeah, Luis is right. I donā€™t remember where I read that though, I just did a quick search on the internet and I didnā€™t found anything about that. :slightly_frowning_face:

@Pablo can you help with this?
Iā€™d like to upgrade to the business plan this site ā†’ devrelfrontity.wordpress.com
So we can still plugins and check how the REST API behaves in wp.com in business mode

After upgrading the site devrefrontity.wordpress.com to a ā€œBusiness planā€ I managed to install some plugins and find out that a new URL is provided as a subdomain of wpcomstaging.com which give you access to all the namespaces of the REST API

I have created a guide to explain what is the REST API URL of a WP site depending on the type of installation