Reputation: 472
Im trying to setup a cron job for all my clients to update their stocks. I've looked into Cron job in WordPress not working and cron job in wordpress but cant get it to appear in the cron list(WP-Control).
im using this:
add_action('plugins_loaded', 'cron_add_everyday');
add_filter( 'cron_schedules', 'cron_add_everyday' );
function cron_add_everyday( $schedules ) {
$schedules = array('everyday' => array( 'interval' => 86400*1,
'display' => __( 'Every Day')),);
return $schedules;
}
Im using this just to test if i can get it to appear in the list. am i missing something? thanks
Upvotes: 2
Views: 1538
Reputation: 472
Ok, so i got it. Full Code:
//CUSTOM TIMER 15 minuts, you can change for your won
function myprefix_custom_cron_schedule( $schedules ) {
$schedules['15Minuts'] = array(
'interval' => 900, // 15 minuts in secounds
'display' => __( '15 minuts' ),
);
return $schedules;
}
//ADD it
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'Your_Function_Hook' ) ) {
wp_schedule_event( time(), '15Minuts', 'Your_Function_Hook' );
}
///Hook into that action that'll fire every 15 minuts
add_action( 'Your_Function_Hook', 'Your_Function' );
//create your function, that runs on the cron you just made
function Your_Function() {
echo 'Hi its 15 minuts';
}
I did not made this, the credits goes to https://wordpress.stackexchange.com/questions/179694/wp-schedule-event-every-day-at-specific-time . Hope it helps some one!
EDIT: if you install a plugin, you can see your cron jobs with a gui, i have wp-control. can check crons at yourdomain/wp-admin/tools.php?page=crontrol_admin_manage_page
Upvotes: 2