0

i want let WordPress endpoint return a HTML-page.

my plugin actually returns Howdy!! if i read web-browser at http://localhost/wordpress/wp-json/a0plugin/v1/testing

i want get a HTML-page instead.

my source returns Howdy!! :

<?php
/**
 * Plugin Name:         a0plugin
 */
function at_rest_testing_endpoint(){
    return new WP_REST_Response('Howdy!!');
}
function at_rest_init(){
    $namespace = 'a0plugin/v1';
    $route     = 'testing';
    register_rest_route($namespace, $route, array(
        'methods'   => WP_REST_Server::READABLE,
        'callback'  => 'at_rest_testing_endpoint'
    ));
}
add_action('rest_api_init', 'at_rest_init');

my tries to get a HTML-page response:

first attempt:
function at_rest_testing_endpoint(){
    return new add_filter( 'page_template', 'myHtml.html' );
}
second attempt:
function at_rest_testing_endpoint()
{
    return add_filter( 'page_template', 'wpa3396_page_template' );

}
function wpa3396_page_template( $page_template )
{
    if ( is_page( 'my-custom-page-slug' ) ) {
        $page_template = 'myHtml.html';
    }
    return $page_template;
}
6
  • 1
    What's the reason for wanting a raw HTML page? Is something preventing you from JSON decoding the response and getting the raw html value that way? I'm a little confused as your first and second attempts do not contain code related to the REST API, and it's not clear what the context for your problem is
    – Tom J Nowell
    May 5, 2021 at 13:17
  • In my test work that I am trying to create, several endpoints are required/expected. i get data from a public json source. html and javascript were enough for me (a attem works without endpoints).
    – SL5net
    May 5, 2021 at 13:28
  • I don't understand why fetching data from a 3rd party resource in JSON would requires a WordPress REST API endpoint to return raw HTML instead of being part of a JSON payload. Can you give more detail or describe in another way? Why do your REST API endpoints need to return raw HTML instead of JSON that contains HTML? This doesn't make sense
    – Tom J Nowell
    May 5, 2021 at 13:40
  • I have to create an html page from a public json source (javascript already does this in my prototypes). that's working. But there have to be different Wordpress endpoints, each using sources a little differently.
    – SL5net
    May 5, 2021 at 13:44
  • 2
    You don't and shouldn't be using the REST API endpoint system to do that. The REST API is for machines to interact with, not humans. People don't use REST APIs, computers do. Just use a page template instead
    – Tom J Nowell
    May 5, 2021 at 14:01

1 Answer 1

1

this was already answered over https://stackoverflow.com/a/50023272/3384980

The default response of WP_HTTP_Response class uses the json_encode() function.

So you want to exit(), rather than return

header('Content-Type: text/html') 
echo '<your html here>'; 
exit();

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.