programing

WordPress 스케줄 이벤트가 설정된 시간 내에 발생하지 않음

starjava 2023. 9. 14. 21:36
반응형

WordPress 스케줄 이벤트가 설정된 시간 내에 발생하지 않음

워드프레스에서는 사용자에게 이메일을 보내는 플러그인을 만들고 있습니다.그래서 워드프레스를 사용하고 있습니다.cron직업. 그래서 기본적으로 이것은 매시간 사용자에게 이메일을 보내는 것입니다.그래서 내 코드는 이렇게 생겼네요.

public function __construct() {
    add_action('init', array( $this, 'send_emails_to_users') );  
    add_action('cliv_recurring_cron_job', array( $this, 'send_email') );
}

public function send_emails_to_users() {
  if(!wp_next_scheduled('cliv_recurring_cron_job')) {
          wp_schedule_event (time(), 'hourly', 'cliv_recurring_cron_job');
    }
}

public function send_email() {
    //send email code goes here
}

여기는 모든 것이 좋아 보이지만 이메일이 전송되지 않습니다.

이렇게 코드를 만들면.

public function __construct() {
    add_action('head', array( $this, 'send_email') );  
}

그러면 이메일이 전송됩니다.그러나 문제는 페이지가 로드될 때마다 또는 사용자가 사이트를 방문할 때마다 이메일을 보낸다는 것입니다.

그래서 제가 사용하고 싶은 것입니다.wp_schedule_event매 시간마다 이메일을 보내요.

그래서 누가 이 문제를 어떻게 해결해야 하는지 알려줄 수 있나요?

어떤 제안이나 도움이라도 주시면 정말 감사하겠습니다.

우선 1) 동적으로 작업하려면 서버에 crontab을 설정해야 합니다 2) 수동으로 워드프레스 스케줄러가 페이지 실행 후 호출합니다

그렇게,

아래의 크론탭 설정이 유용한 링크: 크론탭

cron을 한 시간마다 실행하려면 아래 코드를 추가해야 합니다.

public function __construct() {
    // Call function for cron
    add_action('init', array( $this, 'send_emails_to_users') );
}

public function send_emails_to_users() {
    if(!wp_next_scheduled('cliv_recurring_cron_job')) {
        // Add "cliv_recurring_cron_job" action so it fire every hour
        wp_schedule_event(time(), 'hourly', 'cliv_recurring_cron_job');
    }
}

add_action('cliv_recurring_cron_job', array( $this, 'send_email') );
public function send_email() {
    //send email code goes here
}

자세한 내용은 링크 참조

언급URL : https://stackoverflow.com/questions/36316336/wordpress-schedule-event-not-firing-in-set-time

반응형