WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WordPress and Crontab (7 posts)

  1. paperreduction
    Member
    Posted 4 years ago #

    I am trying to setup the built in wordpress cron functionality (wp-cron.php and includes/cron.php) to run using crontab. In doing so I want to disable the wordpress cron stuff from running on page loads, which I believe is the default functionality.

    I haven't been able to find any good info on doing this. The closest I could find was this thread http://wordpress.org/support/topic/198811.

    I know how to setup crontab, what I really need to know is how the cron stuff in wordpress works. There doesn't seem to be any docs. (I speak php, so technical info is good.)

    Thanks!

  2. MichaelH
    Member
    Posted 4 years ago #

  3. paperreduction
    Member
    Posted 4 years ago #

    Okay, well that gets me pointed in the right direction. I'll role up my sleeves and figure out how it all works. It doesn't look too complicated. I'll be back to post some info and mark this topic as resolved.

    Thanks for the fast reply.

  4. paperreduction
    Member
    Posted 4 years ago #

    Okay so this is what I found:

    wp-settings.php:
    +requires wp-includes/default-filters.php
    -contains the action hook to the function wp_cron()

    +requires wp-includes/cron.php
    -contains functions that perform cron related tasks, including the wp_cron() function
    -above functions make http request to wp-cron.php

    wp-cron.php:
    -actually does the schedule checking and executes anything as needed.

    My solution was to do the following:

    1) In default-filters.php I commented out the following lines:

    //if(!defined('DOING_CRON'))
    //	add_action('init', 'wp_cron');

    This prevents the wp_cron function from being called on every page load, but preserves all other cron functionality in wordpress (as far as I can tell).

    2) I then created a new file in the root directory of wordpress called crontab.php. Here's the basic contents (simplified for brevity):

    <?php
    /** Setup WordPress environment **/
    require_once('./wp-load.php');
    
    /** Call to Run wp-cron **/
    if($_GET["somekey"] == "someEncrypedValue"){
    	wp_cron();
    }
    ?>


    I will be moving this file to a plugin directory to prevent its deletion when wordpress is upgraded, probably wp-crontrol, which i have just installed based on otto42's suggestion. Gotta love otto42!

    3) Pointed crontab to my new crontab.php file. Works like a charm!

    I will say that the docs for this could be a little better. I wouldn't mind putting together a step-by-step how to with a little more robust code.

  5. hakre
    Member
    Posted 4 years ago #

    For no need to comment out something in default-filters.php you can think about defining the DOING_CRON constant in the crontab.php file and set it to some value. Then the default action would not be called and you have no need to uncomment it.

  6. jlbfalcao
    Member
    Posted 4 years ago #

    Or define('DISABLE_WP_CRON', true); at wp-config.php

  7. dreamdevil
    Member
    Posted 4 years ago #

    Thank you guys, thanks to some information here, I have succeed to move scheduled tasks ton a real cron job. To give back to community, I wrote an how-to for both, WordPress and WordPress MU, on my blog here:

    http://blog.dreamdevil.com/index.php/2010/01/29/run-wordpress-tasks-from-real-cron-job/

Topic Closed

This topic has been closed to new replies.

About this Topic