REST API - Head Tags Plugin error with NewsPaper theme

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.

Thanks @mosamlife

Do you have access to the server error logs?

Hi @mosamlife! :wave:

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?

yes you check the error here

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 the wp_head hook has to run several times, one for each entity. The thing is that the NewsPaper theme is registering an action called td_js_generator to the wp_head hook that throws an error when it’s executed more than once. That’s the error you see.

    Fatal error: Uncaught ErrorException:
      - 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 the wp_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 -->
        <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(); ?>

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! :blush:


@David Thanks for the detailed investigation. I already applied my temporary fix for the same but your solution is pretty much straightforward

Thanks :smiley:

1 Like

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? :thinking:


+1 for this @David

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. :smile:

1 Like

Hey, we published the Troubleshooting section in the docs some time ago and I forgot to share the link here! :sweat_smile:

Check it out :point_down:

1 Like

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.