WordPress: schedule multiple action hooks of the same name and time

I am currently trying to call an action hook I have created multiple times within the same time. The reason I am doing this is that the function I am using in the action hook needs to be run for every user with the User ID being passed in as an argument for the function. I also need to have this scheduled in advance, hence why I am using action hooks to schedule this function to be run for every user in advance. The rate at which this function needs to be called is also dependent on the user so I can’t do a set schedule to run it.

So to sum things up:

I need to schedule an action hook with the same name but different arguments. According to the WordPress guide, wp_schedule_single_event should be able to do this so long as the $arg is different. However, no matter how many different $arg values I throw in, only the first one that is created is accepted and the rest are ignored.

Here is the code I have within a button I designed. I am using two dummy arrays to attempt to have 2 different instances of the Action hook run at the same time, but with different arguments (which should work according to the guide but does not).

$action_name = 'Hum'.'8';
                    $action_name2 = 'Hum'.'4';

                    $event_array = array('8',$action_name);
                    $event_array2 = array('4',$action_name2);

                    wp_schedule_single_event($timed4, 'Hope', $event_array);
                    wp_schedule_single_event($timed4, 'Hope', $event_array2);

The other attempt that came really close was the following function I found on the internet from someones attempt to produce the same result:

function uni($ID,$time) {
//This should be able to create a unique action name.
$action_name = 'Hum'.' '.$ID;

$event_array = array($ID,$action_name);
add_action($action_name,'testing');
wp_schedule_single_event($time, $action_name, $event_array);
}

I put this function within the functions.php of my theme and it is able to effectively assign two different action hooks by creating unique names. The problem with this method is that for some reason the scheduled event says the action hook by the value of $action_name does not exist. Even though I added it 1 line prior :. My thoughts on this are that the scope of the add_action within the function is not global and therefore is not obtainable by the wp_schedule_single_event function?

I don’t need the names of the action_hook created to be the same for every user, just that I am able to run the function "testing" from the action hooks created. I just need to get 1 of these methods to work in order to implement my design. Thank you.

Leave a Comment