admin管理员组文章数量:1391956
I have a plugin that creates a WP cron when activated. It runs every hour.
I need to add a simple button somewhere in the dashboard for my client, that would trigger this cron. I don't want the client to do this from plugin's interface (e.g. Cron manager), which might seem too complex for him.
Is there a native WP method or an easy way to run a specific scheduled cron?
I have a plugin that creates a WP cron when activated. It runs every hour.
I need to add a simple button somewhere in the dashboard for my client, that would trigger this cron. I don't want the client to do this from plugin's interface (e.g. Cron manager), which might seem too complex for him.
Is there a native WP method or an easy way to run a specific scheduled cron?
Share Improve this question edited Feb 19, 2020 at 20:59 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Feb 19, 2020 at 7:44 Kristián FiloKristián Filo 4316 silver badges20 bronze badges1 Answer
Reset to default 5When you register a scheduled event for WordPress' cron, the 3rd argument is a hook name:
wp_schedule_event( time(), 'hourly', 'my_hourly_event' );
And you add a function to run on this hook with add_action()
:
add_action( 'my_hourly_event', 'do_this_hourly' );
This will cause do_this_hourly()
to run whenever the scheduled event runs. This works because when the scheduled time occurs, your action is called like this:
do_action( 'my_hourly_event' );
Which causes anything hooked to my_hourly_event
with add_action()
to run. So you can run the hooked function, like do_this_hourly()
, manually at any time by just manually triggering your event with do_action()
, as above, such as in response to a form submission or AJAX request.
Alternatively, you can just run the hooked function directly:
do_this_hourly();
本文标签: How to execute existing WP Cron programmatically
版权声明:本文标题:How to execute existing WP Cron programmatically 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744732962a2622153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论