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:
get_template_part
. When I just call get_template_part
normally on a page, there's no warning. It only appears when it runs in register_rest_route
's callback.register_rest_route
's callback or as an init
action.img
tag, where I'm echo
-ing a URL as the src
using data from the external API response. There are other echo
calls all over this template, so I doubt that's the issue.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 />
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