prosource

3분마다 Word press cronjob 실행

probook 2023. 2. 8. 18:05
반응형

3분마다 Word press cronjob 실행

stackoverflow에 이 주제에 대한 게시물이 많이 있지만 (왜인지) 아무 것도 효과가 없습니다.

내가 가진 것

function isa_add_every_three_minutes( $schedules ) {

    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );

    return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );


function every_three_minutes_event_func() 
{
    // do something
}

wp_schedule_event( time(), 'every_three_minutes', 'every_three_minutes_event_func' );

내가 뭘 잘못하고 있는지 알기나 해?

를 사용해야 합니다.add_action()기능을 스케줄된 이벤트에 후크합니다.

add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );

여기 전체 코드가 있습니다.

// Add a new interval of 180 seconds
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
    wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}

// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
    // do something
}
?>

명령어 add_action()을 사용하지 않은 것 같습니다.

예를 들어, http://wpguru.co.uk/2014/01/how-to-create-a-cron-job-in-wordpress-teach-your-plugin-to-do-something-automatically/ 를 참조해 주세요.

가장 중요한 것은 이 코드가 어디에 있는지 여부입니다.php는 당신의 테마를 위해?아니면 개발 중인 커스텀 플러그인인가요?

제 경험상 커스텀플러그인을 통해 cron을 디버깅하고 활성화하는 것이 쉬워지고 활성화 후크를 사용하여 이벤트를 활성화하고 비활성화합니다.이전에 함수 php를 통해 cron 이벤트를 활성화하는 데 어려움을 겪었으므로 커스텀 플러그인을 통해 이러한 이벤트를 활성화하는 것이 좋습니다.

다음과 같은 플러그인 구조로 시작합니다.

  • /my-brown-my-brown-brow
  • /my-my-displays/index.displays
  • /my-my-mother-my-mother-my-mothern.php

index.disc 내용:

<?php // silence is golden

마이-아저희.php 내용:

<?php

/**
 * Plugin name: My Custom Cron Plugin
 * Description: Simple WP cron plugin boilerplate.
 * Author: Your name
 * Version: 0.1
 */

// Security reasons...
if( !function_exists( 'add_action' ) ){
    die('...');
}

// The activation hook
function isa_activation(){
    if( !wp_next_scheduled( 'isa_add_every_three_minutes_event' ) ){
        wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes_event' );
    }
}

register_activation_hook(   __FILE__, 'isa_activation' );

// The deactivation hook
function isa_deactivation(){
    if( wp_next_scheduled( 'isa_add_every_three_minutes_event' ) ){
        wp_clear_scheduled_hook( 'isa_add_every_three_minutes_event' );
    }
}

register_deactivation_hook( __FILE__, 'isa_deactivation' );


// The schedule filter hook
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );


// The WP Cron event callback function
function isa_every_three_minutes_event_func() {
    // do something
}

add_action( 'isa_add_every_three_minutes_event', 'isa_every_three_minutes_event_func' );

이 플러그인을 설정한 후 플러그인이 활성화되면 이벤트가 활성화됩니다.동작 여부를 테스트하려면 다음 플러그인을 사용합니다.https://wordpress.org/plugins/wp-crontrol/

WP cron의 동작 구조를 이해하는 데 도움이 되는 다른 자원으로는 https://developer.wordpress.org/plugins/cron/을 들 수 있습니다.

언급URL : https://stackoverflow.com/questions/39442982/wordpress-cronjob-every-3-minutes

반응형