REST API - Head Tags plugin not working with WordPress REST API Menus Endpoints

I implemented a WP Menu as suggested here. It’s working correctly, but if I enable your REST API – Head Tags Plugin, I get the following error:

{ FetchError: invalid json response body at http://mysite.com/wp-json/wp/v2/menu-items?menus=2&per_page=100 reason: Unexpected token < in JSON at position 0
    at eval (webpack-internal:///./node_modules/node-fetch/lib/index.mjs:276:32)
    at process._tickCallback (internal/process/next_tick.js:68:7)
message:
'invalid json response body at http://mysite.com/wp-json/wp/v2/menu-items?menus=2&per_page=100 reason: Unexpected token < in JSON at position 0',
type: 'invalid-json' }

TypeError: Cannot read property 'map' of undefined
    at Nav (webpack-internal:///./packages/mars-theme/src/components/nav.js:13:156)
    at runAsReaction (webpack-internal:///./node_modules/@frontity/connect/src/reactionRunner.js:16:45)
    at reaction (webpack-internal:///./node_modules/@frontity/connect/src/observer.js:7:131)
    at props (webpack-internal:///./node_modules/@frontity/connect/src/connect.js:18:8)
    at processChild (webpack-internal:///./node_modules/react-dom/cjs/react-dom-server.node.development.js:480:2385)
    at resolve (webpack-internal:///./node_modules/react-dom/cjs/react-dom-server.node.development.js:479:122)
    at ReactDOMServerRenderer.render (webpack-internal:///./node_modules/react-dom/cjs/react-dom-server.node.development.js:516:1153)
    at ReactDOMServerRenderer.read (webpack-internal:///./node_modules/react-dom/cjs/react-dom-server.node.development.js:516:55)
    at renderToString (webpack-internal:///./node_modules/react-dom/cjs/react-dom-server.node.development.js:555:116)
    at app.use (webpack-internal:///./node_modules/@frontity/core/src/server/index.tsx:45:243)

And this is the raw JSON response:

<br />
<b>Notice</b>:  Undefined index: slug in <b>/cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php</b> on line <b>79</b><br />
<br />
<b>Notice</b>:  Undefined index: slug in <b>/cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php</b> on line <b>79</b><br />
<br />
<b>Notice</b>:  Undefined index: slug in <b>/cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php</b> on line <b>79</b><br />
<br />
<b>Notice</b>:  Undefined index: slug in <b>/cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php</b> on line <b>79</b><br />
<br />
<b>Notice</b>:  Undefined index: slug in <b>/cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php</b> on line <b>79</b><br />
<br />
<b>Notice</b>:  Undefined index: slug in <b>/cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php</b> on line <b>79</b><br />
<br />
<b>Notice</b>:  Undefined index: slug in <b>/cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php</b> on line <b>79</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php:79) in <b>/cphome/mysite.com/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1253</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php:79) in <b>/cphome/mysite.com/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1253</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /cphome/mysite.com/wp-content/plugins/rest-api-head-tags/includes/hooks/class-frontity-headtags-post-type-hooks.php:79) in <b>/cphome/mysite.com/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1253</b><br />
[{"id":15,"title":{"rendered":"Sample Page"},"status":"publish","url":"http:\/\/mysite.com\/sample-page\/","attr_title":"","description":"","type":"post_type","type_label":"Page","object":"page","object_id":2,"parent":0,"menu_order":1,"target":"","classes":[""],"xfn":[""],"meta":[],"menus":[2],"head_tags":[{"tag":"title","content":"mysite.online"},{"tag":"meta","attributes":{"name":"robots","content":"noindex,nofollow"}},{"tag":"link","attributes":{"rel":"dns-prefetch","href":"\/\/s.w.org"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Feed","href":"http:\/\/mysite.com\/feed\/"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Comments Feed","href":"http:\/\/mysite.com\/comments\/feed\/"}}],"_links":{"self":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items\/15"}],"collection":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items"}],"about":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/types\/nav_menu_item"}],"wp:term":[{"taxonomy":"nav_menu","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menus?post=15"}],"wp:object":[{"post_type":"post_type","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/pages\/2"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":16,"title":{"rendered":"Child of Sample Page"},"status":"publish","url":"#","attr_title":"","description":"","type":"custom","type_label":"Custom Link","object":"custom","object_id":16,"parent":15,"menu_order":2,"target":"","classes":[""],"xfn":[""],"meta":[],"menus":[2],"head_tags":[{"tag":"title","content":"mysite.online"},{"tag":"meta","attributes":{"name":"robots","content":"noindex,nofollow"}},{"tag":"link","attributes":{"rel":"dns-prefetch","href":"\/\/s.w.org"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Feed","href":"http:\/\/mysite.com\/feed\/"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Comments Feed","href":"http:\/\/mysite.com\/comments\/feed\/"}}],"_links":{"self":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items\/16"}],"collection":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items"}],"about":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/types\/nav_menu_item"}],"wp:term":[{"taxonomy":"nav_menu","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menus?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":22,"title":{"rendered":"Child of child"},"status":"publish","url":"#","attr_title":"","description":"","type":"custom","type_label":"Custom Link","object":"custom","object_id":22,"parent":16,"menu_order":3,"target":"","classes":[""],"xfn":[""],"meta":[],"menus":[2],"head_tags":[{"tag":"title","content":"mysite.online"},{"tag":"meta","attributes":{"name":"robots","content":"noindex,nofollow"}},{"tag":"link","attributes":{"rel":"dns-prefetch","href":"\/\/s.w.org"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Feed","href":"http:\/\/mysite.com\/feed\/"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Comments Feed","href":"http:\/\/mysite.com\/comments\/feed\/"}}],"_links":{"self":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items\/22"}],"collection":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items"}],"about":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/types\/nav_menu_item"}],"wp:term":[{"taxonomy":"nav_menu","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menus?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":17,"title":{"rendered":"parent 2"},"status":"publish","url":"#","attr_title":"","description":"","type":"custom","type_label":"Custom Link","object":"custom","object_id":17,"parent":0,"menu_order":4,"target":"","classes":[""],"xfn":[""],"meta":[],"menus":[2],"head_tags":[{"tag":"title","content":"mysite.online"},{"tag":"meta","attributes":{"name":"robots","content":"noindex,nofollow"}},{"tag":"link","attributes":{"rel":"dns-prefetch","href":"\/\/s.w.org"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Feed","href":"http:\/\/mysite.com\/feed\/"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Comments Feed","href":"http:\/\/mysite.com\/comments\/feed\/"}}],"_links":{"self":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items\/17"}],"collection":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items"}],"about":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/types\/nav_menu_item"}],"wp:term":[{"taxonomy":"nav_menu","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menus?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":18,"title":{"rendered":"Child 1 of Parent 2"},"status":"publish","url":"#","attr_title":"","description":"","type":"custom","type_label":"Custom Link","object":"custom","object_id":18,"parent":17,"menu_order":5,"target":"","classes":[""],"xfn":[""],"meta":[],"menus":[2],"head_tags":[{"tag":"title","content":"mysite.online"},{"tag":"meta","attributes":{"name":"robots","content":"noindex,nofollow"}},{"tag":"link","attributes":{"rel":"dns-prefetch","href":"\/\/s.w.org"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Feed","href":"http:\/\/mysite.com\/feed\/"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Comments Feed","href":"http:\/\/mysite.com\/comments\/feed\/"}}],"_links":{"self":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items\/18"}],"collection":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items"}],"about":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/types\/nav_menu_item"}],"wp:term":[{"taxonomy":"nav_menu","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menus?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":19,"title":{"rendered":"Child 2 of Parent 2"},"status":"publish","url":"#","attr_title":"","description":"","type":"custom","type_label":"Custom Link","object":"custom","object_id":19,"parent":17,"menu_order":6,"target":"","classes":[""],"xfn":[""],"meta":[],"menus":[2],"head_tags":[{"tag":"title","content":"mysite.online"},{"tag":"meta","attributes":{"name":"robots","content":"noindex,nofollow"}},{"tag":"link","attributes":{"rel":"dns-prefetch","href":"\/\/s.w.org"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Feed","href":"http:\/\/mysite.com\/feed\/"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Comments Feed","href":"http:\/\/mysite.com\/comments\/feed\/"}}],"_links":{"self":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items\/19"}],"collection":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items"}],"about":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/types\/nav_menu_item"}],"wp:term":[{"taxonomy":"nav_menu","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menus?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},{"id":20,"title":{"rendered":"Sample Page"},"status":"publish","url":"http:\/\/mysite.com\/sample-page\/","attr_title":"","description":"","type":"post_type","type_label":"Page","object":"page","object_id":2,"parent":0,"menu_order":7,"target":"","classes":[""],"xfn":[""],"meta":[],"menus":[2],"head_tags":[{"tag":"title","content":"mysite.online"},{"tag":"meta","attributes":{"name":"robots","content":"noindex,nofollow"}},{"tag":"link","attributes":{"rel":"dns-prefetch","href":"\/\/s.w.org"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Feed","href":"http:\/\/mysite.com\/feed\/"}},{"tag":"link","attributes":{"rel":"alternate","type":"application\/rss+xml","title":"mysite.online \u00bb Comments Feed","href":"http:\/\/mysite.com\/comments\/feed\/"}}],"_links":{"self":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items\/20"}],"collection":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menu-items"}],"about":[{"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/types\/nav_menu_item"}],"wp:term":[{"taxonomy":"nav_menu","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/menus?post=20"}],"wp:object":[{"post_type":"post_type","embeddable":true,"href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/pages\/2"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}]

Hi, @koli14!

The problem could be that menu-items is registered as a Custom Post Type in your WordPress site and the REST API - Head Tags plugin is trying to render the head tags for the menus. In order to avoid that, you can add a parameter to the REST API call that is making your custom handler to disable our plugin:

  const response = await api.get({
    endpoint: "menu-items",
    params: {
      menus: id,
      per_page: 100,
      head_tags: false // This one should disable REST API - Head Tags.
    }
  });

Try that and let us know if it works. :blush:

1 Like

Thanks @David, you saved my a** again… :wink:

4 Likes