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 badges
Add a comment  | 

1 Answer 1

Reset to default 5

When 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