I try to set up a cronjob. It seems like it run the le_schedule function but not the le_do_this function. I also tried some cronjob plugins, some says it runs and some says it fails.

In this case it will try to get a file and save it but it doesn't. Why doesn't this work?

add_action( 'wp', 'le_schedule' );
add_action( 'le_event', 'le_do_this' );

function le_schedule() {
    if ( ! wp_next_scheduled( 'le_event' ) ) {
        wp_schedule_event( time(), 'daily', 'le_event');

function le_do_this() {
    $response = wp_remote_get('http://www.example.com/file.txt');
    if( ! is_wp_error( $response ) ) {
        $body = wp_remote_retrieve_body($response);
        if( ! empty( $body ) ) {
            $put = get_template_directory() . '/cache/cache.txt';
            file_put_contents( $put, $body );

wp_clear_scheduled_hook( 'le_event' );
wp_clear_scheduled_hook( 'le_do_this' );

I clear the schedule to be able to run fresh on every pageload.

1 Answer 1


You shouldn't call wp_clear_scheduled_hook on every page load, because then you're always restarting your wp-cron shcedule, with your current setup.

Additionally this call:

wp_clear_scheduled_hook( 'le_do_this' );

doesn't make any difference, since le_do_this isn't a hook name in your setup.

You could try for example this test plugin:

 * Plugin Name: Daily WP-Cron
 * Description: Call the my_daily_cron_script() function daily, if it exists.

add_action( 'mydailyevent', function()
    // Our script:
    if( function_exists( 'my_daily_cron_script' ) )

register_activation_hook( __FILE__, function()            
    // Start the cron job:
    wp_schedule_event( time(), 'daily', 'mydailyevent' );

register_deactivation_hook( __FILE__, function()
    // Stop the cron job:
    wp_clear_scheduled_hook( 'mydailyevent' );

where you have to define the my_daily_cron_script() function to your needs.

There's a warning in the Codex on wp_schedule_event:

The name of an action hook to execute. For some reason there seems to be a problem on some systems where the hook must not contain underscores or uppercase characters.

so let's just use mydailyevent as our hook name, instead of my_daily_event, just in case.

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.