I created a plugin for generating a CSV using the custom fields of certain pages. The generation of the CSV file must occur either by clicking on a button (and this works correctly) or by a cron job that executes the function once a day at a certain time (this does not work).
How can I solve it? or what can I check?
Below I leave you the complete code.
Thank you
<?php
/*
Plugin Name: #
Plugin URI: #
Description: #
Version: 1.0
Author: #
Author URI: #
*/
add_action('admin_menu', 'registra_pagina_opzioni');
add_action('genera_csv_tratte_event', 'genera_csv_tratte');
register_activation_hook(__FILE__, 'schedula_generazione_csv');
register_deactivation_hook(__FILE__, 'rimuovi_schedulazione_csv');
function registra_pagina_opzioni() {
add_management_page(
'Genera Feed CSV',
'Genera Feed CSV',
'manage_options',
'genera-feed-csv',
'pagina_opzioni_genera_feed_csv'
);
}
function pagina_opzioni_genera_feed_csv() {
if (isset($_POST['genera_csv'])) {
genera_csv_tratte();
echo '<div class="updated"><p>Il file CSV è stato generato manualmente.</p></div>';
}
?>
<div class="wrap">
<h2>Genera Feed CSV</h2>
<form method="post" action="">
<input type="submit" name="genera_csv" value="Genera CSV" class="button button-primary">
</form>
</div>
<?php
}
function genera_csv_tratte() {
// code to generate file
}
function schedula_generazione_csv() {
if (!wp_next_scheduled('genera_csv_tratte_event')) {
wp_schedule_event(strtotime('23:00:00'), 'daily', 'genera_csv_tratte_event');
}
}
function rimuovi_schedulazione_csv() {
$timestamp = wp_next_scheduled('genera_csv_tratte_event');
wp_unschedule_event($timestamp, 'genera_csv_tratte_event');
}
If I run the cronjob via a “cronjob manager” plugin, the function runs fine and the file generates, so that’s not the problem.
I deactivated and reactivated the plugin so I could “reset” the existing cron jobs.
1