I'm activating the cron by a form submit button ( not when plugin is activated ). Thus users have choice to set the cron or not.

But it doesn't trigger the function. The cron is set, but triggering the function. I have loaded/visited the site several time after the cron time is passed to load the cron function.

Here is my code,

function obs_register_menu() {
    add_menu_page( .. 'obs_plugin_fn'... ); // obs_plugin_fn is the function for the plugin page

add_action('admin_menu','obs_register_menu'); //add menu

function obs_plugin_fn(){ global $wpdb;

    if(!empty($_POST) && ($_POST['obs_daily_cron_activate']) && !wp_next_scheduled( 'obs_daily_cron' )) { //activate cron on submit
wp_schedule_event(strtotime("now"), 'daily', 'obs_daily_cron');

    if(!empty($_POST) && ($_POST['obs_daily_cron_deactivate']) && wp_next_scheduled( 'obs_daily_cron' )) { // de-activate cron on submit

    if ( !wp_next_scheduled( 'obs_daily_cron' ) ) {

echo '<form action="" method="post">
<input type="submit" name="obs_daily_cron_activate" class="button button-primary" value="Activate daily cron">
</form>'; // submitting this activate the cron
echo '<form action="" method="post">
<input type="submit" name="obs_daily_cron_deactivate" class="button button-primary" value="Deactivate daily cron">
</form>'; // submitting this de-activate the cron

// cron schedule

add_action('obs_daily_cron', 'obs_daily_cron_fn');

function obs_daily_cron_fn() {
// do something every day

} // end plugin function`

1 Answer 1


I figured it out.

I had used the function obs_daily_cron_fn() inside the function obs_plugin_fn(). So the function was not triggered untill the plugin page is opened.

Now moving the function obs_daily_cron_fn() out of the function obs_plugin_fn() works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.