0

I'm using a WP endpoint to request additional post HTML via Javascript. When I query the Endpoint using Postman I'm seeing additional content added to the markup and it's not anywhere in my code. I have a feeling it's PHP erroring but I can't figure out what the problem is.

function get_post($data)
{
    global $post;
    $post = get_post($data['id']);
    $previous_post = get_previous_post();
    $post = $previous_post;
    $markup = include('includes/template.php');
    return $markup;
}
add_action('rest_api_init', function () { // Add API endpoint on init
    register_rest_route('query-post', '/post/(?P<id>\d+)', array( 
        'methods' => 'GET',
        'callback' => 'get_post',
        'permission_callback' => '__return_true'
    ));
});

What is being returned from the API is my rendered template with correct post data, but there is a random 1 at the end of the markup.

<div class="post">
    Hello World!                            
</div>1

1 Answer 1

0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.