8

I'm creating a cron job that runs outside of WordPress. From a development perspective, this makes it really easy to debug and create cleanup scripts or cron jobs, by just running a command like the following on the command line:

php myfile.php

My issue is that WordPress somehow delays all output... typically when you run php files like this the output is generated on your screen in real time.

Does anyone know how to prevent WP from delaying all the output until the end of the job? It's probably some simple parameter I'm missing when running the PHP file, or a constant of some kind, but I'm not sure.

In the following code, you'd expect to have a 1-second delay between each output, instead, I currently get all the info printed at the end, at once:

set_time_limit( 0 );
ini_set( "memory_limit", "64M" );

ob_start();
require_once( dirname( __FILE__ ) . '/wp-load.php' ); // you need to adjust this to your path
ob_end_clean();

global $wpdb;
$q = $wpdb->prepare ("SELECT * FROM wp_posts LIMIT 5");
$results = $wpdb->get_results ($q);
echo "found " . count ($results) . "\n";

foreach ($results as $post)
{
    echo $post->post_title . "\n";
    sleep(1);
}

echo "\n\ndone";
2
  • I believe it has to do with the settings on the server and not wordpress itself. Tested your code and it works as intended on my server when running from the command-line. Do you have any plugins that cache maybe?
    – Kokarn
    Commented Mar 7, 2012 at 14:35
  • @Kokarn you are correct. I should have tested this in another WP install. Looks like it's because I'm using W3 Total Cache. Will post the eventual solution back here (instead of just deactivating the plugin).
    – Alex Cook
    Commented Mar 9, 2012 at 3:07

2 Answers 2

11
+25

After some testing indeed, both WP Super Cache and W3 Total Cache do not release the buffer (or prevent the buffer from being released).

Turning off "output delay" is simple and depends on the caching plugin involved.

WP Super Cache:

wp_cache_disable();
ob_end_flush(); // or ob_end_clean();

This should be added after including wp-load.php, this stops any caching dead in its tracks and flushes the buffer.

W3 Total Cache:

ob_end_clean(); // or ob_end_flush();

Same as above, call after including wp-load.php, similarly to what you have there; should work with just flushing, W3 Total Cache does start buffer, however it does not appear to prevent its flushing per se. Judging by the way your output is still cached and flushing does not work, I'd say you have WP Super Cache, which is more aggressive.

Make sure that you don't start a new buffer before including wp-load.php, otherwise you'll have a buffer within a buffer (bufferception?), and will have to flush twice or more.

6
  • Thanks - this answer is particularly helpful with the code sample. Haven't had time to test yet but going to check right now.
    – Alex Cook
    Commented Mar 9, 2012 at 3:00
  • I'm using W3 Total Cache. Looks like "wp_cache_disable" isn't a valid function - is that specific to Super Cache?
    – Alex Cook
    Commented Mar 9, 2012 at 3:06
  • Yes, wp_cache_disable() is particular to WP Super Cache. I'll look into W3 Total Cache to see how it can be disabled in a similar way, using ob_end_flush worked for me. What are your W3 Total Cache settings and version?
    – soulseekah
    Commented Mar 9, 2012 at 16:06
  • I've edited my answer.
    – soulseekah
    Commented Mar 9, 2012 at 16:10
  • Make sure that you don't start a new buffer before including wp-load.php, otherwise you'll have a buffer within a buffer.
    – soulseekah
    Commented Mar 9, 2012 at 16:31
4

Disable any plugins you may have that do whole-page caching. WP-Super-Cache, W3 Total Cache, etc.

WordPress does not "delay output". But whole page caching plugins usually do. This is because they are trying to get that output and save it somewhere, for later usage in serving the page. Thus, the page output is delayed until the end, where the plugin can cache it.

1
  • Ahhh, good catch. Should have thought of that! I'll test and get back to you. Thanks.
    – Alex Cook
    Commented Mar 7, 2012 at 22:12

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.