如何通过插件的自定义功能使用wordpress cron作业? [英] How to use wordpress cron job with a custom function from the plugin?

查看:91
本文介绍了如何通过插件的自定义功能使用wordpress cron作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为woocommerce开发自定义插件.在该插件中,功能就像它将每24小时获取所有用户并向他们发送电子邮件.因此,即使不访问网站,也要每24小时进行一次工作,我认为我需要这项工作.因此,通过搜索wordpress cron作业,我得到了 wp_schedule_event 函数.但是我在这里不明白如何使用它?我的意思是该功能将如何工作这里?我有自定义功能来获取所有用户并向他们发送电子邮件,但是如何与 wp_schedule_event 一起工作,以及

I am devloping a custom plugin for woocommerce. In that plugin the functionality is like it will get all the users and send them email in every 24 hrs. So doing a work in every 24hrs even without visiting a site I think I need a cron job for that. So by googling over wordpress cron job I got wp_schedule_event function. But here I can't understand how to work with it? I mean how the function will work here? I have my custom function to get all the users and send them email but how to work that function with wp_schedule_event and how the wp_schedule_event function should be call from the plugin so that if no one even visits the site it will work silently. Any help and suggestion will be really appreciable. Thanks

推荐答案

您需要将插件功能映射到操作,然后将该操作安排到Wordpress cron.

You need to map your plugin function to an action, and then schedule that action to the Wordpress cron.

// ---- Schedule cron event for custom plugin function.
add_action('init', 'custom_plugin_function_event');
function custom_plugin_function_event() {
    if (wp_next_scheduled('custom_plugin_action') == false) {
        wp_schedule_event(time(), 'daily', 'custom_plugin_action');
    }
    add_action('custom_plugin_action', 'custom_plugin_function');
}

// ---- Execute custom plugin function.
function custom_plugin_function() {
    // Do something here
}

上面的示例用于过程代码.将 custom_plugin_function()替换为插件函数的名称.

The above example is for procedural code. replace custom_plugin_function() with your plugin function's name.

这篇关于如何通过插件的自定义功能使用wordpress cron作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆