最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp cron - How to force 'cron_schedules' every minute instead 1 hour?

programmeradmin0浏览0评论

I have a problem because I try to run my function every minute...

And there are my two codes which I tried:

function my_cron_schedules($schedules){
    if(!isset($schedules["61sec"])){
        $schedules["61sec"] = array(
            'interval' => 61,
            'display' => __('Every 61 sec'));
    }
    return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
 
add_action('wp','setup_schedule');
function setup_schedule() {
  if (!wp_next_scheduled('expire_ogl') ) {
    wp_schedule_event( current_time('timestamp'), '61sek' , 'expire_ogl');
  }
}

add_action( 'expire_ogl', 'expire_ogl_now' );
function expire_ogl_now() {
update_post_meta(1,'now',date());
}

I tried to do also this:

add_action('wp','setup_schedule');
    function setup_schedule() {
      if (!wp_next_scheduled('expire_ogl') ) {
        wp_schedule_event( current_time('timestamp'), 'every_minute' , 'expire_ogl');
      }
    }
    
    add_action( 'expire_ogl', 'expire_ogl_now' );
    function expire_ogl_now() {
    update_post_meta(1,'now',date());
    }

But when I check planned schedules - I get always that it will be runed every hour (even If I set '61sec' or 'every_minute'. Can somone help me? Thank you in advance.

I have a problem because I try to run my function every minute...

And there are my two codes which I tried:

function my_cron_schedules($schedules){
    if(!isset($schedules["61sec"])){
        $schedules["61sec"] = array(
            'interval' => 61,
            'display' => __('Every 61 sec'));
    }
    return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
 
add_action('wp','setup_schedule');
function setup_schedule() {
  if (!wp_next_scheduled('expire_ogl') ) {
    wp_schedule_event( current_time('timestamp'), '61sek' , 'expire_ogl');
  }
}

add_action( 'expire_ogl', 'expire_ogl_now' );
function expire_ogl_now() {
update_post_meta(1,'now',date());
}

I tried to do also this:

add_action('wp','setup_schedule');
    function setup_schedule() {
      if (!wp_next_scheduled('expire_ogl') ) {
        wp_schedule_event( current_time('timestamp'), 'every_minute' , 'expire_ogl');
      }
    }
    
    add_action( 'expire_ogl', 'expire_ogl_now' );
    function expire_ogl_now() {
    update_post_meta(1,'now',date());
    }

But when I check planned schedules - I get always that it will be runed every hour (even If I set '61sec' or 'every_minute'. Can somone help me? Thank you in advance.

Share Improve this question edited Jan 13, 2021 at 5:40 Dariusz asked Jan 13, 2021 at 5:31 DariuszDariusz 12 bronze badges 1
  • 2 I’m voting to close this question because it's incomplete and OP requested deletion. – rudtek Commented Jan 13, 2021 at 5:45
Add a comment  | 

1 Answer 1

Reset to default 3

You have the wrong period alias in your code

You added your personal period in my_cron_schedules() as 61sec:

$schedules["61sec"] = array(

but use it in setup_schedule() as 61sek:

wp_schedule_event( current_time('timestamp'), '61sek' , 'expire_ogl');

The other parts of the code look right.

发布评论

评论列表(0)

  1. 暂无评论