Current WordPress version 5.4
I also tried with deactivating all the plugin still got the same error. but when I am hitting the endpoint 4 to 5 times itâs working. but with frontity connect it is throwing 500 error.
Hi @mosamlife!
Since you mention this, it seems to be the error on the server-side. To expand on what @Pablo has mentioned, if you can paste the log from the server that would help us troubleshoot it.
Is there any more helpful information in the client-side error in the browser console?
Hello @Pablo @mmczaplinski Thanks for quick reply. I found the solution It was my current theme that causing the issue with rest api head tags plugin. Current theme : NewsPaper
May we know what was it? Is it something we could solve?
Could you please check if you have the latest version of the Head Tags plugin?
I am already using the latest version
Ok, thanks @mosamlife. I guess the affected hook is wp_head
then. Can we download the theme from somewhere to check it out?
Sure! I will send you in DM
Hi @mosamlife!
I did some tests with the theme you shared in DM and I was able to reproduce the problem. It looks like the NewsPaper theme is not compatible with the Head Tags plugin for some reasons:
-
The REST API - Head Tags plugin runs the
wp_head
hook for each post, page, etc. in order to render their tags. This means that for the same REST API request thewp_head
hook has to run several times, one for each entity. The thing is that the NewsPaper theme is registering an action calledtd_js_generator
to thewp_head
hook that throws an error when itâs executed more than once. Thatâs the error you see.Fatal error: Uncaught ErrorException: td_js_buffer::add_to_header - the header JS was already rendered when you called td_js_buffer::add_to_header()
-
Apart from that, the NewsPaper theme is using a
header.php
template that renders the title outside of thewp_head
hook, making that tag unable to be processed by the Head Tags plugin, and therefore not showing that tag in the REST API response.<!-- header.php --> <head> <meta charset="<?php bloginfo( 'charset' );?>" /> <!-- The title is rendered here --> <title><?php wp_title('|', true, 'right'); ?></title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" /> <!-- The theme is calling `wp_head` here --> <?php wp_head(); ?> </head>
Having said that, you can add the following code as a workaround in the functions.php
of your theme that would remove the action that throws the error and add the title only when a REST API request is made. I think itâs safe to remove the td_js_generator
action as it only adds JavaScript code that the Head Tags plugin wonât return anyways.
add_action('rest_api_init', function() {
// Remove the failing hook.
remove_action('wp_head', 'td_js_generator', 10);
// Add a new hook for the <title> tag.
add_action('wp_head', function () {
echo '<title>' . wp_title('|', false, 'right') . '</title>';
});
});
I hope this workaround works for you. Cheers!
@David Thanks for the detailed investigation. I already applied my temporary fix for the same but your solution is pretty much straightforward
Thanks
Hey @David, would it make sense to create a list of disabled hooks in our plugin and add this one remove_action('wp_head', 'td_js_generator', 10);
to the list?
I know this is going to be hard to maintain but at least we can add problematic hooks when a problem arises.
I think it would be better to let people know why those plugins are failing and how to make it work. Maybe with a âtroubleshootingâ section in the plugin docs or something like that.
What do you think?
Sorry for the delay guys. If you think a troubleshooting section is better then itâs fine. Letâs add it.
@David can you take care of it?
Sure! Iâll link it here once itâs done.
Hey, we published the Troubleshooting section in the docs some time ago and I forgot to share the link here!
Check it out
https://docs.frontity.org/frontity-plugins/rest-api-head-tags#troubleshooting
Hello. I came across the same error as mentioned by @mosamlife on my newspaper theme. I have added the workaround script into the function.php file, however it still doesnât work. the same error still persist. may I know how do I continue?
@eching97 can you switch to Yoast 14 and @frontity/yoast
? I think right now itâs a better option.
- The package: https://www.npmjs.com/package/@frontity/yoast
- A snippet you need to add to your WP: Support for Yoast plugin REST API fields