Wordpress cronjob каждые 3 минуты
есть много сообщений в 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' );
Любые идеи, что я делаю неправильно?
Ответы
Ответ 1
Вам нужно использовать 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 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
}
?>
Ответ 2
Кажется, вы забыли использовать команду add_action()
См. этот пример: http://wpguru.co.uk/2014/01/how-to-create-a-cron-job-in-wordpress-teach-your-plugin-to-do-something-automatically/
Ответ 3
В первую очередь важно, где находится этот код, есть ли в вашей функции .php для вашей темы? Или это настраиваемый плагин для разработки?
По моему опыту, он как можно легче отлаживает и активирует cron через собственный плагин, используя активационные крючки для активации и деактивации событий. У меня были жесткие времена, активирующие события cron через функции php раньше, я предпочитаю активировать эти события через пользовательские плагины.
Я бы начал с такой структуры плагина, как это:
- /мой-хрон-плагин
- /my-cron-plugin/index.php
- /my-cron-plugin/my-cron-plugin.php
Содержание index.php:
<?php // silence is golden
Содержимое my-cron-plugin.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/