I’ve looked at multiple other questions about this warning (Headers already sent…etc) and this thorough explanation, but I’m not finding a solution for the circumstances I’m dealing with:
- I have a custom WordPress REST API endpoint that returns the output of
get_template_part
. When I just callget_template_part
normally on a page, there’s no warning. It only appears when it runs inregister_rest_route
‘s callback. - I am also fetching data from an external API using this library. The error started when I started making these requests. I’ve tried making the request both inside and outside the template part, the latter either within
register_rest_route
‘s callback or as aninit
action. - The line that triggers the error is an
img
tag, where I’mecho
-ing a URL as thesrc
using data from the external API response. There are otherecho
calls all over this template, so I doubt that’s the issue. - The line in question actually works fine and does its job. I just need to get rid of the accursed warning.
Code:
Inside functions.php:
<?php //the api endpoint declaration add_action( 'rest_api_init', function () { register_rest_route( 'theme/v2', // namespace '/homepage', // route array( // options 'methods' => 'GET', 'callback' => 'build_home', ) ); }); //the callback for the wordpress api function build_home() { // this is the external API request, using their PHP library. // I linked the library above. The request returns an object with a bunch of data include_once(get_template_directory()."/arena/arena.php"); $arena = new Arena(); $page = $arena->set_page(); $per = 8; $slug = 'concepts-bo-6ajlvpqg'; $channel = $arena->get_channel($slug, array('page' => $page, 'per' => $per)); //I'm passing the response from the external API (in $channel) to get_template part //get_template_part is then the output for the wordpress API request get_template_part('custom-home-template',null,array('channel'=>$channel)); } ?>
Inside template part:
<?php $channel=$args["channel"]; ?> <div id="concepts-box" class="bodycontent reg-width"> <?php foreach ($channel->contents as $item) { if($item->class=="Image"){ $img=$item->image["square"]["url"]; ?> <div class="concept-wrapper"> <!-- the line below is the one that triggers the error --> <img lozad src="<?=$img; ?>"> </div> <?php }} ?> </div>
Full warning:
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /filepath/wp-content/themes/theme/custom-home-template.php:63) in <b>/filepath/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1372</b><br />
Advertisement
Answer
Answered on WordPress SO — link here. The issue was I was treating get_template_part
as an API response instead of feeding it through output buffering into a variable