duanbei2914 2016-12-18 08:27
浏览 41

如何在wordpress中编写真正的cron作业?

I have done define('DISABLE_WP_CRON', 'true'); in wp-config.php.

I set up the cron path but I actually do not know how to write code so that calling that URL will execute my code. I want to write real cron job over WordPress cron job.

I tried with this in function.php but it did not work:

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
}
  • 写回答

1条回答 默认 最新

  • download1214 2016-12-18 13:47
    关注

    Here is a link to the post that solve it for me: http://support.hostgator.com/articles/how-to-replace-wordpress-cron-with-a-real-cron-job

    Takeaways:

    • You seem to have prepared everything correctly - your code schedules the execution of do_this_hourly function every hour. However, due to define('DISABLE_WP_CRON', 'true'); in wp-config.php the function is only scheduled and never executed unless you make a request to http://yourwebsite.com/wp-cron.php?doing_wp_cron yourself.
    • The only thing left to do is to setup system cron
    • If you are on Unix-based system (Linux/Mac), then try crontab -e from the command line on your server, and add a line like this: wget -q -O - http://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

    Don't forget to replace http://yourwebsite.com with whatever your website domain is.

    Good luck!

    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致