The problem is within the get_post
function.
After looking through the documentation for include()
and require()
, which includes the *_once()
variations of the functions, the problem seemed to be that it was returning a Boolean on whether the template had been included/required or not without throwing an error.
After changing how the template was requiring/including it’s template files, the REST API function was still including the additional Boolean value as part of the return statement, as a printed 1
.
What was causing the problem was actually the return
statement. The PHP documentation for Return states:
return returns program control to the calling module. Execution resumes at the expression following the called module’s invocation.
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.
If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call.
The return
statement was ending execution of the function and returning the value being returned by the executed function. So I updated my function to only return
once execution of the include
function was completed.
NOTE I had removed the return
from the function entirely and it returned the expected value but I added it to end execution of the get_post
function.
function get_post($data)
{
global $post;
$post = get_post($data['id']);
$previous_post = get_previous_post();
$post = $previous_post;
include('includes/template.php');
return;
}