如何使用wp cron插入数据

时间:2015-07-24 作者:Pavnish Yadav

我在teble中每1分钟使用wp\\U schedule\\u事件插入数据。但这里有一些混乱,我必须添加插入代码。这是我的代码,其中包含了一些附加函数。php在我的主题中。

register_activation_hook(__FILE__, \'my_activation\');
add_action(\'my_hourly_event\', \'do_this_hourly\');
function my_activation() {
    wp_schedule_event(time(), \'minutes\', \'my_hourly_event\');

        global $wpdb;
       //INSERT INTO `wp_test`.`wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES (NULL, \'database_options\', \'hello\', \'yes\');
}

function do_this_hourly() {
     $schedules[\'every_three_minutes\'] = array(
            \'interval\'  => 60,
            \'display\'   => __( \'Every 1 Minutes\', \'textdomain\' )
        );

    return $schedules;
} 

//deactive
register_deactivation_hook(__FILE__, \'my_deactivation\');
function my_deactivation() {
    wp_clear_scheduled_hook(\'my_hourly_event\');
}

2 个回复
最合适的回答,由SO网友:Vee 整理而成

First you can to create a schedule for one minute, then bind function with that hook.

function add_new_intervals($schedules) 
{
    // add weekly and monthly intervals
    $schedules[\'every_single_minutes\'] = array(
        \'interval\' => 60,
        \'display\' => __(\'Every Minute\')
    );
    return $schedules;
}
add_filter( \'cron_schedules\', \'add_new_intervals\');

// To schedule event for this minute event
wp_schedule_event( current_time( \'timestamp\' ), \'every_single_minutes\', \'my_every_minute_event\');

//add_action(\'my_every_minute_event\', \'_Func_to_execute_\');

check this quick reference for details

Thanks

SO网友:Benoti

您的cron必须在插件或主题激活中注册。尝试wp crontol插件来验证并运行您的cron任务。但是,我认为您可以调用“分钟”间隔,在这里您可以调用“每三分钟”间隔来安排事件。

结束