Rushi Jagani
Rushi Jagani

Reputation: 160

Wordpress cron schedule can be set using variable?

I add a custom interval as below

when I submit the from then i got the value using $_POST

add_filter( 'cron_schedules', 'my_cron_custom_hook' );
        function my_cron_custom_hook( $schedules ) {

      if (isset($_POST['expiry'])) {
          $settings = $_POST['expiry'];
           $expiry = $settings['expiry']; -- > got the value
       }  

   $schedules['specific_time'] = array(
        'interval' => $expiry,--> (Got value here)
         'display' => __('Specific Time')
    );
}

In this case i got the value of $expiry

 add_filter( 'cron_schedules', 'my_cron_custom_hook' );
      function my_cron_custom_hook( $schedules ) {

       $settings = get_option( 'from_value' ); 
       $expiry = $settings['expiry'];
       $schedules['specific_time'] = array(
      'interval' => $expiry,--> (can't get the value here)
       'display' => __('Specific Time')
    );
}

I can't get the value of $expiry

when i click the submit button twise then only $expiry value is visible. what is the solution for this?

Upvotes: 0

Views: 192

Answers (1)

Benoti
Benoti

Reputation: 2210

Your form need to save $expiry first in the options table. For this, use an action (ie: admin_init if the form is validate in the admin side) that can fire this code,

 add_action('admin_init', 'save_expiry');

 function save_expiry(){

     if(isset($_POST['expiry']){
         update_option('from_value[expiry]', $_POST['expiry']);
     }
 }

Then, in the cron_schedules filter, you'll be able to get the option value.

$settings = get_option('from_value');
$expiry = (!isset($settings['expiry']) && $settings['expiry']=='')? 'daily' : $settings['expiry'];

Upvotes: 1

Related Questions