Wp_schedule_event () in the plugin without scheduling a cron event

I create a WordPress plugin when the plugin is activated. I need the cron job to be scheduled to run every 5 minutes.

Here is my code;

// Register plugin activation hook
function my_plugin_activate() {
    if( !wp_next_scheduled( 'my_function_hook' ) ) {  
       wp_schedule_event( time(), '5', 'my_function_hook' );  
    }
}
register_activation_hook( __FILE__, 'my_plugin_activate' );

// Register plugin deactivation hook
function my_plugin_deactivate(){
    wp_clear_scheduled_hook('my_function_hook');
}
register_deactivation_hook(__FILE__,'my_plugin_deactivate');

// Function I want to run when cron event runs
function my_function(){
    //Function code
}
add_action( 'my_function_hook', 'my_function');

When I use this plugin https://wordpress.org/plugins/wp-crontrol/ to check for cron events, nothing has been added, I expect the cron event to be added, that "my_function" is executed at 5 minute intervals, I have no mistakes

+6
source share
3 answers

See: wp_schedule_event()

- , . cron_schedules wp_get_schedules().

, 5 .

<?php // Requires PHP 5.4+.

add_filter( 'cron_schedules', function ( $schedules ) {
    $schedules['every-5-minutes'] = array(
        'interval' => 5 * MINUTE_IN_SECONDS,
        'display'  => __( 'Every 5 minutes' )
    );
    return $schedules;
} );

if( ! wp_next_scheduled( 'my_function_hook' ) ) {  
    wp_schedule_event( time(), 'every-5-minutes', 'my_function_hook' );  
}
+5

WP Cron, - . , , cron .

2 :

  • WP Cron, cron .

https://support.hostgator.com/articles/specialized-help/technical/wordpress/how-to-replace-wordpress-cron-with-a-real-cron-job

  1. wp_schedule_event():

    function myprefix_custom_cron_schedule( $schedules ) {
        $schedules['every_six_hours'] = array(
            'interval' => 21600, // Every 6 hours
            'display'  => __( 'Every 6 hours' ),
        );
        return $schedules;
    }
    add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
    
    //Schedule an action if it not already scheduled
    if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
        wp_schedule_event( time(), 'every_six_hours', 'myprefix_cron_hook' );
    }
    
    ///Hook into that action that'll fire every six hours
     add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );
    
    //create your function, that runs on cron
    function myprefix_cron_function() {
        //your function...
    }
    

http://www.nextscripts.com/tutorials/wp-cron-scheduling-tasks-in-wordpress/

http://www.iceablethemes.com/optimize-wordpress-replace-wp_cron-real-cron-job/

http://www.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/

Wp cron

http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules

http://www.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/

http://www.viper007bond.com/2011/12/14/how-to-create-custom-wordpress-cron-intervals/

http://www.sitepoint.com/mastering-wordpress-cron/

https://tommcfarlin.com/wordpress-cron-jobs/

http://www.paulund.co.uk/create-cron-jobs-in-wordpress

cron linux

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

http://www.thesitewizard.com/general/set-cron-job.shtml

http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

google search

+1

@dingo_d, WordPress Cron , Cron. WordPress Cron . , , . , - 5- , . - -, .

, WordPress - Cron .

, , , Cron script .

, linux , crontab -e enter. cron cron. :

/5 * * * wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron

http://yourdomain.com . , 5 .

, https://tommcfarlin.com/wordpress-cron-jobs/, WordPress cron.

0

All Articles