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 "nndone";
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:
This should be added after including
wp-load.php
, this stops any caching dead in its tracks and flushes the buffer.W3 Total Cache:
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.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.