Endpoint written with add_rewrite_endpoint() returns headers already sent

Hi all,

I am trying to integrate a BroadBean (Job feed) plugin that imports jobs using an endpoint. We currently have this plugin installed on a staging site to set it up and troubleshoot issues first.

I have been investigating the issue of why the endpoint: /jobrelay/jobfeed/ is not returning JSON data and is being intercepted by our Roots theme and is rendering the wp_head() markup which is not what is intended. It is assumed that the framework interprets this page as a WP template and attempts to render the page with HTML.

Switching over to the 2022 WP theme returns this endpoint as JSON.
I also used a fresh install of the Roots theme, disabled all other plugins, and re-installed the WP database and the same issue occurred.

On further investigation, we inspected the Job Feed plugin code (which is very well commented) and discovered that the endpoints.php file uses a WP function: add_rewrite_endpoint() to build this URI. We have been told not to modify the plugin as we will obviously not be able to update it with future releases.

Is there a way to ensure that this specific route (/jobrelay/jobfeed/ ) is not interpreted as a template by Roots and we force it to return JSON data only?

I’m happy to provide more details if required.

Which version of Sage is your theme built on?

These are the version details found in style.css:

Description:        Sage is a WordPress starter theme.
Version:            10.5.1

Sage 10 means you’re probably using Acorn, so I’m guessing that your issue has to do with Acorn intercepting things at template_include:

https://github.com/roots/acorn/blob/1c697d542741b58c92061bf84df465858a5cba6f/src/Roots/Acorn/Sage/Concerns/FiltersTemplates.php#L20-L44

If that is the case, then you might be able to resolve your problem by either unhooking Acorn from that filter when you know you’re running on that endpoint, or by intercepting the filter once Sage is done with it, and returning whatever correct data you want to return.

1 Like

That’s right @alwaysblank, we are using “roots/acorn”: “^3.1” in the theme.

You mentioned that I can try unhooking Acorn from that filter and provided a link to the SageServiceProvider class.

I managed to prevent the boot method from running when the route /jobrelay/jobfeed/ is called in the ThemeServiceProvider class and it is now returning JSON data.

Thanks for pointing me in the right direction.

2 Likes