43

I've just installed Wordpress 4.7. One of the mentions in the announcement is that it features a REST API.

My understanding is:

  • The Wordpress REST API plugin is not needed anymore, because supposely it has already been merged with core in Wordpress 4.7.
  • The Wordpress REST API is enabled by default

However, I notice that in a vanilla Wordpress 4.7 install, none of the endpoints seems to work (for example http://examples.com/wp-json/wp/v2/posts).

How can I tell if the new REST API is really activated? Can the REST API be enabled or disabled with the vanilla Wordpress UI, or should I install a third party plugin to do so?

6
  • 1
    Is there discovery tag in page source? <link rel='https://api.w.org/' href='https://example.com/wp-json/' /> Seems to just work on my sites.
    – Rarst
    Dec 10, 2016 at 17:32
  • @Rarst, if I grep -r "<link rel='https://api.w.org/'" . in the htmldirectory, it returns one hit in file ./wp-includes/rest-api.php with this text: echo "<link rel='https://api.w.org/' href='" . esc_url( $api_root ) . "' />\n";
    – Pep
    Dec 10, 2016 at 18:13
  • Not in your source code, in the HTML source of a page from your site in browser.
    – Rarst
    Dec 10, 2016 at 18:16
  • 1
    @Rarst, yes I see <link href="http://example.com/wp-json/" rel="https://api.w.org/">
    – Pep
    Dec 10, 2016 at 18:24
  • What does opening the link in browser get you? With WP_DEBUG enabled?
    – Rarst
    Dec 10, 2016 at 18:32

4 Answers 4

29

4.7 has it enabled by default. The easy way to check if it is working is just to visit the example.com/wp-json url, and you should get a list of registered end points there

There is no official option to disable it as (at least there was a talk about it not sure if it got in the release), some core functionality depends on it.

The most obvious things to check for if it is not working is your htaccess rules, and do you have a wp-json directory

Also, if /wp-json/wp/v2/posts type URLs don't work for you, but /?rest_route=/wp-json/wp/v2/posts does, it means you need to enable pretty permalinks in the settings (suggested by Giles Butler in comments below).

10
  • I do not have a wp-json directory under /var/www/html or its subdirectories. I do not seem to have a .htaccess file (other than /var/www/html/wp-content/plugins/akismet/.htaccess, should I have one?
    – Pep
    Dec 10, 2016 at 17:54
  • @Pep, what webserver do you use? do you have pretty permalinks on? Dec 10, 2016 at 17:58
  • Apache. I followed exactly the steps in these tutorials to install Wordpress 4.7 on AWS: docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-LAMP.html and docs.aws.amazon.com/AWSEC2/latest/UserGuide/…
    – Pep
    Dec 10, 2016 at 18:04
  • 1
    I had the permalink with the "Plain" settings. I changed it to "Post name". Wordpress could not save the .htaccess file apparently because of permissions, so it gave me the code. I created a .htaccess file using ssh with that content. However, the REST API continues to return 404, even for http:example.com/wp-json/
    – Pep
    Dec 10, 2016 at 18:29
  • 8
    I found finally the issue. Because I did not know that Permalink would be required for the REST API to work, I skipped during initial setup the step to set AllowOverride All in the <Directory "/var/www/html">section of /etc/httpd/conf/httpd.conf. That's why it was ignoring my changes to .htaccess. Now with a permalink other than PLain, the REST API endpoints work.
    – Pep
    Dec 10, 2016 at 19:53
13

I had 4.7 also thought that REST API was disabled, but I was tricked by the URL. To see the correct URL seek a line looking something like that:

link rel='https://api.w.org/' href='http://mysite?rest_route=/' />. So, using http://mysite?rest_route=/ as the prefix solved my problem. For instance to recover the posts is enought to type: http://mysite?rest_route=/wp/json

I couldn't find on documentation that the query param was needed. Was I the only one?

5
  • Worked for me with wordpress 4.8.x Not sure why this is not documented anywhere.
    – Anirudha
    Aug 31, 2017 at 11:35
  • 2
    Not the only one. Only found this answer in your comment after 10min searching. Needed to do: www.example.com?rest_route=/wp/v2/posts
    – Eugene K
    Feb 7, 2018 at 3:50
  • 5
    I think this happens when pretty permalinks aren't enabled. From the docs... "On sites without pretty permalinks, the route is instead added to the URL as the rest_route parameter. For the above example, the full URL would then be example.com/?rest_route=/wp/v2/posts/123" Jul 19, 2018 at 21:36
  • here it is -> developer.wordpress.org/rest-api/#routes-endpoints Jan 3, 2019 at 16:48
  • Very useful if you are testing out using PHP builtin sever php -S localhost:1234. Sep 2, 2020 at 2:22
7

If REST API isn't working out-of-box or after a fresh install and after typing in

mydomain/wp-json/wp/v2/posts 

then you would need to simply activate your "permalinks" as post_name located:

WP Dashboard->Settings->Permalinks

Or if you do not wish to activate permalinks, you can simply type:

mydomain?rest_route=/wp/v2/posts

Man, I wish WordPress would update their Rest handbook to be more user-friendly. I like the old version of docs :)

1

I see you have fixed the issue but leaving my solution here as it worked for me too. I had this same issue when updating from beta15 to the core api in wp 4.7 Turns out the issue was that I had some plugins that were using a deprecated function register_api_field which I changed to register_rest_field according to this note in the changelog:

BREAKING CHANGE: Rename register_api_field() to register_rest_field().

Introduces a register_api_field() function for backwards compat, which calls _doing_it_wrong(). However, register_api_field() won't ever be committed to WordPress core, so you should update your function calls.

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.