I am trying to schedule wordpress cron job having dynamic hooks. I am developing a plugin and the cron jobs are to be scheduled dynamically. I need a way to name the hooks that directly corresponds to logged in user. So i am appending user login name as hook name. Here is the piece of code that i am using.
class Easy_Editor_Settings {
public $script="";
public $default_email="";
public $default_name="";
public $hook="";
public function __construct() {
global $current_user;
wp_get_current_user();
$current_username=$current_user->user_login;
$this->hook="easy_user".$current_username;
add_action ($this->hook, [$this,'run_service'], 1, 10 );
add_action( 'wp_ajax_easy_email_settings_call_back', [$this,'easy_email_settings_call_back'] );
add_filter('cron_schedules',[$this,'my_cron_schedules']);
}
The public function run service only prints error log but takes in 10 arguments those i have to use later. I can see the job being scheduled using WP-Control plugin. The problem is when i use hook name by appending $current_username variable at the end, the run_service function does not run. But Wp-Control shows the action to required function along with arguments and hook's name. However, when i append get_current_user() as
$this->hook="easy_user".get_current_user();
The run_service function works, however, get_current_user returns my OS username instead of wp username. I am not sure what is causing the issue, i need some way to append current logged in user in hook name.