How to Insert data with wp cron
-
03-10-2020 - |
Question
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');
}
Solution
First you can to create a schedule for one minute, then bind function with that hook.
function add_new_intervals($schedules)
{
// add weekly and monthly intervals
$schedules['every_single_minutes'] = array(
'interval' => 60,
'display' => __('Every Minute')
);
return $schedules;
}
add_filter( 'cron_schedules', 'add_new_intervals');
// To schedule event for this minute event
wp_schedule_event( current_time( 'timestamp' ), 'every_single_minutes', 'my_every_minute_event');
//add_action('my_every_minute_event', '_Func_to_execute_');
check this quick reference for details
Thanks
OTHER TIPS
your cron must be register at the plugin or theme activation. Try wp crontol plugin to verify and run your cron task. But, i think you call "minutes" interval where you might call "every_three_minutes" interval for the schedule event.
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange