REST API - Head Tags
It is perfect if you are using WordPress for a headless set-up and you would like to add the meta tags generated by your WordPress SEO plugin (like Yoast SEO or All-in-One SEO Pack) to the WordPress REST API output.
This plugin is compatible and works out of the box with the following WordPress SEO plugins:
- Yoast SEO
- All in One SEO Pack
The plugin has been developed to include the
head_tagsfield to the REST API response of most of the WordPress core entities:
- Posts, pages, attachments and custom post types
- Post types: for archive pages
- Categories, tags and custom taxonomies
You need to understand better how it works and add the data manually.
You have to get each entity from its respective REST API endpoint.
For example, for fetching the posts you should go to
/wp-json/wp/v2/posts&id=123endpoint, for fetching the categories you have to go to
wp-json/wp/v2/categories&id=123,and for custom post types or custom taxonomies would be a different URL in each case.
In the case of the homepage, it could be less intuitive and you should go to
/wp-json/wp/v2/types/post.As previously said, each entity has a different endpoint so if you aren't familiar with this, you should check the WordPress REST API reference for more info.
Inside each endpoint, it will be a new field named
head_tags, which will be an array of objects representing the tags that WordPress would normally include inside the HTML
headelement. These objects have the properties
For example for these HTML tags:
<title>Hello wordl! - My Site</title>
<meta name="robots" content="max-snippet:-1, max-image-preview:large, max-video-preview:-1">
<link rel="canonical" href="<http://mysite.com/hello-world/>" />
This would be the content of the
"content": "Hello world! - My Site"
"content": "max-snippet:-1, max-image-preview:large, max-video-preview:-1"
The settings of this plugin are really simple.
In order to not affect the performance of your web, the
head_tagsfield is cached for all your responses, but we've added a button to purge this cache in case something changes.
By default, the
head_tagsfield is included in the common endpoint of each entity. You can configure it so it doesn't appear by default and to be shown when you include the
For example, with the output disabled,
https://mysite.com/wp-json/wp/v2/postswon't show the
head_tagsfield unless you have the query
? head_tags=trueat the end.
In case you want to skip the cache, you can do so by adding to the query the parameter
There are some cache plugins for the REST API that also use the same parameter. In case you want to ignore the cache for the REST API call but not for the head tags, you can use
- 1.First of all you have to install the plugin. You can do it:
- Automatic: from within the WordPress dashboard go to Plugins, click the
Add Newbutton, search for
REST API - Head Tags(by Frontity) and click
- 2.Once installed, you have to activate it and it will be running!
- This could happen with some plugins and themes that add hooks to the
wp_headaction.What the REST API - Head Tags plugin does is to call the
wp_headaction for every entity contained in the REST API response and transform the generated HTML code into a JSON object. That means, any hook registered to that action could be causing the problem if it:
You can check these topics in the community for specific cases that might help you.
- generates invalid HTML
- unexpectedly ends the PHP execution
- throws an exception
- Another reason why it could return an error is when you make a request to a custom post type endpoint that is not expected to have the
head_tagsfield (like the
menu-itemsendpoint).The solution is just disable the REST API - Head Tags plugin output when making those requests, simply adding
head_tags=falseto the query, as explained in REST API - Head Tags plugin not working with WordPress REST API Menus Endpoints.
- One possibility is that the WordPress theme or plugin generates those fields in a different action than
wp_head. This is not easy to solve, you would have to find what hooks add the missing fields and attach them to
wp_head, or write those hooks yourself.A known case for this is the NewsPaper theme, wich adds the
header.phpdirectly - without using the
wp_headaction. You can take a look at REST API - Head Tags Plugin error with NewsPaper theme for a specific solution if you are using this theme.
- This problem could also happen if there is no theme at all in your WordPress site, because themes are normally what add those tags inside
<head>. You can try using one that comes with WordPress by default to fix this.
- Also, if you are using a plugin or any other system to cache the REST API responses you can try also clearing the cache.