Skip to main content
edited tags
Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459
Source Link

How to Insert data with wp cron

I am using wp_schedule_event for Insert data every 1 Minutes in a teble. But here is some confusion where i have to add code for Insert. Here is my code which add in functions.php in my theme.

register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');
function my_activation() {
    wp_schedule_event(time(), 'minutes', 'my_hourly_event');
        
        global $wpdb;
       //INSERT INTO `wp_test`.`wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES (NULL, 'database_options', 'hello', 'yes');
}

function do_this_hourly() {
     $schedules['every_three_minutes'] = array(
            'interval'  => 60,
            'display'   => __( 'Every 1 Minutes', 'textdomain' )
        );
     
    return $schedules;
} 

//deactive
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation() {
    wp_clear_scheduled_hook('my_hourly_event');
}