1

How can I prevent Headers from being sent?

I am trying to list (WordPress) blog posts on a non-WordPress site (both sites share a public web folder).

I want to run the following PHP code to get the blog posts:

        <ul class='list-unstyled'>
        <?php
            define('WP_USE_THEMES', false);
            require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');
            query_posts('showposts=5');
            while (have_posts()): the_post();
            ?>
                <li><i class='glyphicon glyphicon-menu-right'></i>
                    <h4><a href="<?php the_permalink(); ?>" class="myred text-main"><?php the_title(); ?></a></h4>
                    <p><?php the_date(); ?> | Category: <?php the_category(','); ?> | <?php the_tags(); ?></p>
                </li>
            <?php endwhile; ?>
        </ul>

However, it seems running this code sends payload headers to client. Is there any way to retrieve the blog posts without sending headers?

5
  • You could use the REST API to access the WP posts without sending headers. v2.wp-api.org/reference/posts
    – WebElaine
    Commented May 30, 2017 at 18:49
  • @webElaine - Can this be done on the server side? I am not familiar with using PHP to make remote calls, and I wish to avoid AJAX for SEO purposes. Commented May 30, 2017 at 18:53
  • yes i can - developer.wordpress.org/rest-api/using-the-rest-api/… Thanks, @webElaine Commented May 30, 2017 at 18:56
  • what is the error you are actually getting? Commented May 31, 2017 at 3:48
  • The initial error is "Headers already sent", which is a well documented PHP error, see answers below. Based on feedback from @WebElaine I am currently working on a code snippet that will use the Wordpress Rest API to get the content I'm wanting to display on my non WP site. Commented May 31, 2017 at 19:10

3 Answers 3

1

"Headers already sent" is not a WordPress error, it is PHP processing error message. Good explanation here; https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php .

I suspect it might be caused by some characters (probably space characters) sent before your <?php code segment. Even space (or tab [code formatting] ) characters will cause an error.

And perhaps move your <ul> code to just above the while loop.

2
  • yes i'm aware of the php error, but why should wordpress care if headers are sent. I have html content above my UL, so moving the while loop would not solve my problem. Now, I reckon I could move the loop to above all of my content, and save output to a string value to be printed later in the appropriate spot, but that still begs the question as to why WP needs to send headers if i'm not rendering the whole WP site. Commented May 30, 2017 at 22:57
  • 2
    @JeffWilkerson because WordPress was not designed to do what you are doing, it thinks you are rendering the whole site.
    – Milo
    Commented May 31, 2017 at 1:32
0

Output of the page started as your php is executing. Look into into output buffering.

Putting a ob_start() function call before any HTML in the file and a call to ob_get_clean() after should solve the problem.

It would look something like this:

<?php

define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');
//Buffer output instead of immediately sending to the client
//make sure there is only PHP here
ob_start();
?>
//templating stuff...
        <ul class='list-unstyled'>
    <?php
        query_posts('showposts=5');
        while (have_posts()): the_post();
        ?>
            <li><i class='glyphicon glyphicon-menu-right'></i>
                <h4><a href="<?php the_permalink(); ?>" class="myred text-main"><?php the_title(); ?></a></h4>
                <p><?php the_date(); ?> | Category: <?php the_category(','); ?> | <?php the_tags(); ?></p>
            </li>
        <?php endwhile; ?>
    </ul>
//More templating stuff...
<?php
ob_get_clean();
//output starts
?>

Also as Rick Hellewell pointed out in his answer, make sure the file starts with nothing but an opening <?php tag. No blank space, not HTML, nothing.

5
  • I still get an ugly php error saying headers already sent. Why does Wordpress care so much about headers? Commented May 30, 2017 at 19:06
  • It's not WordPress it is a PHP error. Take a look at Rick Hellewell's answer. He raises a good point I should have been more clear on. You need to make sure there is nothing before the opening <?php tag of the file. No white space, not html, nothing. Then before the ob_start() you can't add anything but PHP
    – DaveLak
    Commented May 30, 2017 at 19:55
  • See my updated answer, I forgot a ?> after the ob_start(); and a <?php before the ob_get_clean();. Also re organized things a bit. Sorry :-)
    – DaveLak
    Commented May 30, 2017 at 20:03
  • ok. Interesting. I see what is going on. thanks for clarifying. Commented May 31, 2017 at 19:05
  • Yup, let us know if this helped.
    – DaveLak
    Commented May 31, 2017 at 20:15
0

I solved my problem by installing the REST API plugin, then calling the end point from my non-wordpress site, caching the results, then displaying the blog list from my cache results.

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.