Skip to content

Instantly share code, notes, and snippets.

@jmarreros
Created January 30, 2018 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmarreros/997f46a3969529af792be5b471d9c6d2 to your computer and use it in GitHub Desktop.
Save jmarreros/997f46a3969529af792be5b471d9c6d2 to your computer and use it in GitHub Desktop.
Código de ejemplo de cómo crear un cron en WordPress
<?php
/*
Plugin Name: Ejemplo Cron
Description: Plugin para mostrar cómo funciona el cron de WordPress
Version: 1.0
Author: Jhon Marreros Guzmán
Author URI: http://decodecms.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// Activación del Plugin
register_activation_hook( __FILE__, 'dcms_plugin_activation' );
function dcms_plugin_activation() {
if( ! wp_next_scheduled( 'dcms_my_cron_hook' ) ) {
wp_schedule_event( current_time( 'timestamp' ), '5seconds', 'dcms_my_cron_hook' );
}
}
// Desactivación del plugin
register_deactivation_hook( __FILE__, 'dcms_plugin_desativation' );
function dcms_plugin_desativation() {
wp_clear_scheduled_hook( 'dcms_my_cron_hook' );
}
// Acción personalizada
add_action( 'dcms_my_cron_hook', 'dcms_my_process' );
function dcms_my_process() {
error_log('Mi evento se ejecutó: '.Date("h:i:sa"));
}
//Registro de intervalos
add_filter( 'cron_schedules', 'dcms_my_custom_schedule');
function dcms_my_custom_schedule( $schedules ) {
$schedules['5seconds'] = array(
'interval' => 5,
'display' =>__('5 seconds','dcms_lang_domain')
);
return $schedules;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment