Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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";
share|improve this question
    
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 Mar 7 '12 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 Mar 9 '12 at 3:07
add comment

2 Answers

up vote 8 down vote accepted
+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.

share|improve this answer
    
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 Mar 9 '12 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 Mar 9 '12 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 Mar 9 '12 at 16:06
    
I've edited my answer. –  soulseekah Mar 9 '12 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 Mar 9 '12 at 16:31
show 1 more comment

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.

share|improve this answer
    
Ahhh, good catch. Should have thought of that! I'll test and get back to you. Thanks. –  Alex Cook Mar 7 '12 at 22:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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