0
votes

I am having a hard time figuring out this issue. So basically, I wrote a plugin.. the file structure is as below

-myplugin.php (init and includes myplugin-common.php )
-myplugin-admin.php (plugin menu item index and includes myplugin-part1.php and part2)
-myplugin-common.php
-myplugin-part1.php
-myplugin-part2.php

i have a function that activates the cron if it is not already enabled .. example below (this is inside the myplugin.php)

function myplugin_cron_activate() {

    if ( get_option( SCHEDULE_ENABLED ) !== false ) {
        if ( get_option(SCHEDULE_ENABLED) == 'true') {
            if (!wp_next_scheduled('wpo_cron_event2')) {

                $schedule_type = get_option(SCHEDULE_TYPE, 'weekly');

                wp_schedule_event(time(), $schedule_type, 'my_cron_event2');
                add_action('my_cron_event2', 'my_cron_action');

        }
        }
    }

now *my_cron_action* is inside a common file myplugin-common.php . Which is fine and it works anyway..

The problem is, myplugin-part2.php is a settings page to enable or disable the scheduler. When I enable it .. calls the *myplugin_cron_activate()* function. But it does not activate the cron function in wordpress. But when I go to the plugin main link in admin - it gets activated.

Please note both myplugin-part1.php, myplugin-part2.php are included from inside the myplugin-admin.php. myplugin-admin.php holds 2 tabs and loads these 2 part of files accordingly.

What I still can't figure out why the scheduler is not getting activated when i submit the settings page - yes i am calling the function there on $POST. But it gets activated when I click on the main link for the admin page.. (because of this I can't show the next schedule time either)

1

1 Answers

0
votes

You need to add weekly schedule

function add_weekly_cron( $schedules ) {
        $schedules['weekly'] = array(
        'interval' => 604800,
        'display' => __('Once Weekly','beforeafter')
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'add_weekly_cron' ); 

Also, I think you're triggerring wp-cron in a wrong way

add_action('my_action_hook','my_cron_function');
if( !wp_next_scheduled( 'my_cron function' ) ) {  
   wp_schedule_event( time(), 'weekly', 'my_action_hook' );  

Use Cron View to debug WP-CRON :)