動的に生成されたアクション名を使用して、WordPressCronAPIで複数のイベントをスケジュールしようとしています。一度に追加できるイベントは1つだけadd_action()
ですが、複数は追加できません。
Cron Viewというプラグインを使用すると、イベントが登録されていることを確認できましたが、関数が何らかの理由で呼び出されませんでした。動的に生成されたアクション名が次のページの読み込みで消えて、WordPressがそれらを見つけることができないのではないかと推測しています。知らない。
次のコードはプラグインとして実行する準備ができており、問題を示しています。この問題を解決するための提案をいただければ幸いです。
<?php
/* Plugin Name: Sample Cron Scheduler */
add_action('admin_menu', 'sample_cron_menu');
function sample_cron_menu() {
add_options_page(
'Sample Cron Scheduler',
'Sample Cron Scheduler',
'manage_options',
'sample_cron_scheduler',
'sample_cron_scheduler_admin');
}
function sample_cron_scheduler_admin() {
?>
<div class="wrap">
<?php
// print the saved values
echo '<h3>Saved values</h3>';
print_r(get_option('crontest'));
// do this ten times, meaning I want to register 10 events
$chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
for ($i=1; $i <= 10; $i++) {
// generate a random string
shuffle($chars);
$randomstring = implode($chars);
// schedule the next event
// wp_schedule_single_event(time(), 'myeventaction'); // <-- this only runs one job
add_action(sha1($randomstring),'myevent'); // so I want to add arbitrary multiple events but this doesn't work
wp_schedule_single_event(time(), sha1($randomstring));
}
?>
</div>
<?php
}
add_action('myeventaction', 'myevent');
function myevent() { // this function just adds the current time to the option array
$arr = get_option('crontest') ? get_option('crontest') : array();
array_push($arr, date("M d Y H:i:s", time())); // adds the current time at the end of the array
array_splice($arr, 0, count($arr) - 20); // reduce the number of elements to 20 to make it short
update_option('crontest', $arr);
}