admin管理员组文章数量:1122846
I'm Trying to scheduling wp_cron job in my website to send email everyday without opening my website but this is not happening. This work when user come to the website or reload a page.
Here is my code to scheduling cron job
// WP_CRON Scheduling
function my_custom_cron_schedule( $schedules ) {
$schedules['every_one_minute'] = array(
'interval' => 60, // Every 1 Minute
'display' => __( 'Every 1 Minute' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'my_custom_cron_schedule' );
//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'my_cron_function_hook' ) ) {
wp_schedule_event( current_time('timestamp'), 'every_one_minute', 'my_cron_function_hook' );
}
///Hook into that action that'll fire every One Minute
add_action( 'my_cron_function_hook', 'my_cron_function' );
//create your function, that runs on cron
function my_cron_function() {
$crtime = date('F-j-Y h:i:s');
wp_mail('[email protected]','Cron Job Test', "$crtime");
}
I'm Trying to scheduling wp_cron job in my website to send email everyday without opening my website but this is not happening. This work when user come to the website or reload a page.
Here is my code to scheduling cron job
// WP_CRON Scheduling
function my_custom_cron_schedule( $schedules ) {
$schedules['every_one_minute'] = array(
'interval' => 60, // Every 1 Minute
'display' => __( 'Every 1 Minute' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'my_custom_cron_schedule' );
//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'my_cron_function_hook' ) ) {
wp_schedule_event( current_time('timestamp'), 'every_one_minute', 'my_cron_function_hook' );
}
///Hook into that action that'll fire every One Minute
add_action( 'my_cron_function_hook', 'my_cron_function' );
//create your function, that runs on cron
function my_cron_function() {
$crtime = date('F-j-Y h:i:s');
wp_mail('[email protected]','Cron Job Test', "$crtime");
}
Share
Improve this question
edited Jun 14, 2019 at 15:20
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Jun 13, 2019 at 10:51
Vishal tanwarVishal tanwar
15 bronze badges
1 Answer
Reset to default 1Wordpress Cron Jobs are not "real" Cronjobs, instead they rely on somebody visiting the site, then looking if it's time to do scheduled jobs.
- Someone visits the website
- WordPress makes a non-blocking request to
wp-cron.php
wp-cron.php
looks into the database if there are scheduled jobs that are due at this time.wp-cron.php
does all of the scheduled jobs that are due.
If you want to make sure that your cronjob is executed on time, you need to do some more steps:
- Schedule your cronjob. You did that already.
- Disable the integrated WordPress Cron triggering. Edit your
wp-config.php
and insert the linedefine('DISABLE_WP_CRON', true);
below the linedefine('DB_COLLATE','');
- Create a "real" cronjob on your server, set to run every so often (5 Minutes, 15 Minutes, 1 Minute, something like that). I don't know if and how you can create a cronjob on your hosting, that's something you got to ask your hosting company. Make the target of your cronjob
yourdomain.com/wp-cron.php
- Success! Your cron should work now as intended!
Happy Coding!
本文标签: WordPress CRON job working when reloading the page
版权声明:本文标题:WordPress CRON job working when reloading the page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736295357a1929554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论