A website I’m working on has a membership system, and on the first day of each month we essentially need to run a custom function declared in functions.php that will go through the active subscribers and update a user meta value for each active subscriber.
The function is in place and working, but I’m trying to figure out how to schedule it correctly.
​
[https://developer.wordpress.org/reference/functions/wp\_schedule\_event/])​
The above looks promising, but the examples are for setting it to be daily, or every 30 days, etc, where as I need it specifically on the 1st day of the month to be ran.
I guess one thought I had would be running the function daily, but adding a check inside the function to determine if its the first day of the month? So it would cancel out 29\~ times, and then complete one once?
Is there a smarter way to do this?
[ad_2]
You can add custom wp-cron intervals but as far as I am aware these are just intervals like every 30 days and won’t allow you to schedule for a specific day.
Running the function daily with a date check would be a valid approach.
Another approach would be periodically using wp_schedule_single_event() to schedule an event for the 1st of the next month (use wp_next_scheduled() to avoid duplicates):
Yet another would be to wrap your function call up in a simple wp-cli command and trigger it via system cron
wp_cron operates on intervals and there isn’t an interval that runs on the first day of each month.
Something to think about is whether you’d like your function to execute when no one visits your site as both wp_cron and wp_schedule_single_event() only run when someone visits your site.
If you’d like to run your function regardless, you could try something like:
0 0 1 * * php /home/site/somefunction.php