0

I want to remove all HTML from a page generated by Wordpress.

It needs to be simple plain text, without any HTML, at all.

When opening the page in the browser, it should not return any HTML code. Therefore, I do not want to edit a post -- I want to completely remove everything.

I have only found ways to replace the post content. However, I do not want to replace the post content. I want to completely remove the entire HTML of a page. Starting from <!DOCTYPE html> and ending with </html>.

1 Answer 1

3

You could make a template for this in your active theme, then assign that page to use the new template file.

null-template.php

<?php
/**
 * Template Name: Null
 */
 
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        the_title();
        echo( wp_strip_all_tags( get_the_content() ) );
    }
}

Then create a new page (or edit the one you want to be HTML-free) and make sure it's using the Null template.

Screenshot using the "Null" template

This should result in a page that has no HTML whatever in it.

References

5
  • Thanks. That already points at the right direction. Almost everything is removed. Now only an html tag and a body tag are still artificially generated, because the editor forces me to enter text in the frontend editor, which, as I understand it, forces a proper HTML file with a body. Could not get the following solution to work. stackoverflow.com/a/20922186/7061105
    – Akito
    Oct 4, 2022 at 0:08
  • 1
    I tried this solution on a local installation, using the block editor and the Twenty Twenty Two theme, and the source of the generated page had no HTML tags at all.
    – Pat J
    Oct 4, 2022 at 2:24
  • 1
    "the editor forces me to enter text in the frontend editor, which, as I understand it, forces a proper HTML file with a body" No, that's not how any of this works. Are you actually getting an <html> tag? Or is the dev tools just showing one? Check the Network tab to see the raw data returned by the server. Oct 4, 2022 at 6:42
  • 1
    Well, crap. @JacobPeattie is absolutely correct. I just had checked the dev tools. Didn't know, it is unprecise in such a scenario. I curled the link and it actually works. Thanks to both of you for helping me. This solution works great.
    – Akito
    Oct 5, 2022 at 18:12
  • To future readers: in case anyone wants to change the Content-Type of this page's response, one must append a header('Content-Type: text/plain; charset=utf-8'); (or whatever Content-Type is required) after the <?php depicted in the answer. For example: <?php header('Content-Type: text/plain; charset=utf-8');
    – Akito
    Oct 5, 2022 at 19:06

Your Answer

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

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