WP_Schedule_Event/cron_Schedules-插件中的自定义重复时间不起作用

时间:2012-01-15 作者:LeMoussel

我试图在插件中为WP cron添加自定义间隔。它以“小时”、“每日”默认重复时间运行。当我为函数wp\\u schedule\\u事件添加自己的自定义重复时间时,它不会运行。

我想每X小时执行一个函数(由用户定义)。

谢谢

代码如下:

<?php
/*
Plugin Name: Test Cron
Description: Plugin to Test Cron Jobs.
Version: 1.00
*/
class WP_TestCron {
    function __construct()
    {
        register_activation_hook(__FILE__, array($this, \'TestCron_cron_activate\'));
        register_deactivation_hook(__FILE__, array($this, \'TestCron_cron_deactivate\'));
        add_filter(\'cron_schedules\', array($this, \'TestCron_cron_update_schedules\'));
        add_action(\'TestCron_cron_eventOne\', array($this, \'TestCron_cron_fct\'));
        add_action(\'TestCron_cron_eventTwo\', array($this, \'TestCron_cron_fct\'));
    }

    function TestCron_cron_activate() 
    {
        // Run => In Schedule Cronjobs 
        if (!wp_next_scheduled(\'TestCron_cron_eventOne\')) { wp_schedule_event(time(), \'hourly\', \'TestCron_cron_eventOne\'); }

        // Dont\'t Run => Not in Schedule Cronjobs 
        if (!wp_next_scheduled(\'TestCron_cron_eventTwo\')) { wp_schedule_event(time(), \'in_per_minute\', \'TestCron_cron_eventTwo\'); }
    }

    function TestCron_cron_deactivate() 
    {
        wp_clear_scheduled_hook(\'TestCron_cron_eventOne\');
        wp_clear_scheduled_hook(\'TestCron_cron_eventTwo\');
    }

    function TestCron_cron_update_schedules() 
    {
        return array(
            \'fively\' => array(\'interval\' => 300, \'display\' => \'Once in Five minutes\'),
            \'in_per_minute\' => array(\'interval\' => 60, \'display\' => \'In every minute\'),
            \'in_per_ten_minute\' => array(\'interval\' => 60 * 10, \'display\' => \'Once in Ten minutes\'),
            \'three_hourly\' => array(\'interval\' => 60 * 60 * 3,  \'display\' => \'Once in three hours\')
        );  
    }

    function TestCron_cron_fct() 
    {
        $testCcron_post = array();
        $testCcron_post[\'post_title\'] = \'Cron Update - \'.date(\'d/m/y \\a\\t h:i A\');
        $testCcron_post[\'post_content\'] = \'WP-Cron job is working!\';
        $testCcron_post[\'post_status\'] = \'publish\';
        $testCcron_post[\'post_author\'] = 1;
        $testCcron_post[\'post_category\'] = array(1);

        wp_insert_post( $testCcron_post );  
    }   
}

if (is_admin()) $wpTC = new WP_TestCron();

?>

2 个回复
SO网友:TheDeadMedic

WP Cron不在管理范围内运行-删除if ( is_admin() ) 条件

SO网友:Lars Koudal

我还建议不要覆盖现有的时间表。

正确的形式是:

function filter_cron_schedules( $schedules ) {
$schedules[\'fively\'] = array(
    \'interval\' => 300,
    \'display\' => __(\'Once in Five minutes\')
);  

$schedules[\'in_per_minute\'] = array(
    \'interval\' => 60,
    \'display\' => __(\'In every minute\')
);  

$schedules[\'in_per_ten_minute\'] = array(
    \'interval\' => 600,
    \'display\' => __(\'Once in Ten minutes\')
);  

$schedules[\'three_hourly\'] = array(
    \'interval\' => 10800,
    \'display\' => __(\'Once in three hours\')
);  
return $schedules;
}
它将获取现有的$计划,向其中添加4个新的间隔,然后返回更新的数组。

请注意,我以前在基于类的插件中遇到过一些关于cron的严重问题,最近的一个问题让我想到了这个问题。

结束

相关推荐

__FILE__ in WordPress plugins

我正在使用Sidebar Generator 在我所有的主题中。该插件在“plugins”目录下正常工作,但我想以某种方式将其嵌入到我的主题中,因此它不会显示在已安装的插件列表中。我刚刚复制了sidebar\\u生成器。php(幸运的是,整个代码都在一个文件中)到mytheme/插件,并将其包含在函数中。phprequire_once (MY_PLUGINS . \'/sidebar_generator.php\'); 现在,它适用于所有其他插件,但不适用于这个插件!这样,当我点击“侧栏”时