【问题标题】:Schedule Multiple Arbitrary Events to WP Cron in WordPress在 WordPress 中将多个任意事件安排到 WP Cron
【发布时间】:2012-09-07 07:34:11
【问题描述】:

我正在尝试使用具有动态生成的操作名称的 WordPress Cron API 来安排多个事件。 add_action() 一次只能添加一个事件,但不能超过一个。

使用名为Cron View 的插件,我可以确认事件已注册,但未以某种方式调用该函数。我猜可能动态生成的动作名称在下一页加载中消失了,所以 WordPress 找不到它们。我不知道。

以下代码可以作为插件运行并演示问题。任何解决此问题的建议将不胜感激。

<?php
/* Plugin Name: Sample Cron Scheduler */

add_action('admin_menu', 'sample_cron_menu');
function sample_cron_menu() {
    add_options_page(
        'Sample Cron Scheduler', 
        'Sample Cron Scheduler', 
        'manage_options',
        'sample_cron_scheduler', 
        'sample_cron_scheduler_admin');
}
function sample_cron_scheduler_admin() {
    ?>
    <div class="wrap">
    <?php
        // print the saved values
        echo '<h3>Saved values</h3>';
        print_r(get_option('crontest'));

        // do this ten times, meaning I want to register 10 events 
        $chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
        for ($i=1; $i <= 10; $i++) {

            // generate a random string
            shuffle($chars);
            $randomstring = implode($chars);

            // schedule the next event
            // wp_schedule_single_event(time(), 'myeventaction');   // <-- this only runs one job
            add_action(sha1($randomstring),'myevent');              // so I want to add arbitrary multiple events but this doesn't work
            wp_schedule_single_event(time(), sha1($randomstring));                  
        }
    ?>
    </div>
    <?php
}

add_action('myeventaction', 'myevent'); 
function myevent() {    // this function just adds the current time to the option array

    $arr = get_option('crontest') ? get_option('crontest') : array();
    array_push($arr, date("M d Y H:i:s", time()));      // adds the current time at the end of the array
    array_splice($arr, 0, count($arr) - 20);            // reduce the number of elements to 20 to make it short
    update_option('crontest',  $arr);

}

【问题讨论】:

    标签: php cron wordpress


    【解决方案1】:

    我的猜测是对的。 WordPress 不记得在 add_action() 中传递的注册操作名称。所以我将它们保存在选项中并在插件加载时检索它们并将它们全部传递给add_action()解决了它。

    <?php
    /* Plugin Name: Sample Cron Scheduler */
    
    add_action('admin_menu', 'sample_cron_menu');
    function sample_cron_menu() {
        add_options_page(
            'Sample Cron Scheduler', 
            'Sample Cron Scheduler', 
            'manage_options',
            'sample_cron_scheduler', 
            'sample_cron_scheduler_admin');
    }
    function sample_cron_scheduler_admin() {
        ?>
        <div class="wrap">
        <?php
            // print the saved values
            echo '<h3>Saved values</h3>';
            echo '<h4>Actions</h4>';
            print_r(get_option('crontest_actions'));
            echo '<h4>Jobs</h4>';
            print_r(get_option('crontest'));
    
            // do this ten times, meaning I want to register 10 events 
            $arr = get_option('crontest_actions') ? get_option('crontest_actions') : array();
            $chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
            for ($i=1; $i <= 10; $i++) {
    
                // generate a random string
                shuffle($chars);
                $randomstring = implode($chars);
    
                // schedule the next event
                $actionname = sha1($randomstring);
                array_push($arr, $actionname);
                add_action($actionname,'myevent');              // so I want to add arbitrary multiple events but this doesn't work
                wp_schedule_single_event(time(), $actionname);                  
            }
            update_option('crontest_actions', $arr);
        ?>
        </div>
        <?php
    }
    
    $actions = get_option('crontest_actions') ? get_option('crontest_actions') : array();
    foreach($actions as $key => $action)    {
        add_action($action, 'myevent'); 
    }
    
    function myevent() {
    
        $arr = get_option('crontest') ? get_option('crontest') : array();
        array_push($arr, date("M d Y H:i:s", time()));      // adds the current time at the end of the array
        array_splice($arr, 0, count($arr) - 20);            // reduce the number of elements to 20 to make it short
        update_option('crontest',  $arr);
    
        $actions = get_option('crontest_actions') ? get_option('crontest_actions') : array();
        array_splice($actions, 0, 1);   // remove the first element
        update_option('crontest_actions',  $actions);
    }
    
    // use this to reset options
    // $arr = array();
    // update_option('crontest', $arr);
    // update_option('crontest_actions', $arr);
    

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 2018-09-23
      • 1970-01-01
      • 2012-09-16
      • 2020-12-18
      • 2019-03-27
      • 2021-06-03
      • 2017-01-04
      • 1970-01-01
      相关资源
      最近更新 更多