I have a function which has got data from the API. I want to run this function in every day at a specific time. I have used below code for achieving this, but the blow of run every min. I am using this code on the 5min interval for scheduled tests. My API code is, too much long. I am using just test purpose example code there.
register_activation_hook(__FILE__, 'my_activation');
add_action( 'wp', 'my_activation' );
function my_activation() {
if (! wp_next_scheduled ( 'my_hourly_event' )) {
wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}
}
add_action('my_hourly_event', 'do_this_hourly');
function do_this_hourly() {
// do something every hour
$t = time();
$user_login = 'test-'.$t;
$user_email = 'test-'.$t.'@yahoo.com';
$user_pass = '123456';
$userdata = compact( 'user_login', 'user_email', 'user_pass' );
$user_id = wp_insert_user( $userdata ) ;
// On success.
if ( ! is_wp_error( $user_id ) ) {
echo "User created : ". $user_id;
}else{
echo "<h1>User already exsist</h1>";
}
}
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation() {
wp_clear_scheduled_hook('my_hourly_event');
}