[ad_1]
Hey there!
I’m creating my first plugin and I have been stuck in one issue for the past few days. Hopefully someone can help me find out what’s failing!
This is the problematic piece of code:
//Function to run weekly
function wp_stocks_update_all_stocks() {
wp_mail('[email protected]', 'Automatic email', 'Weekly cron wp_stocks_update_all_stocks has been succesfully executed!');
}
//If event is not scheduled, do it. Adjusted to every_min while in development.
if (!wp_next_scheduled('wp_stocks_weekly_event')) {
wp_schedule_event( time(), 'every_min', 'wp_stocks_weekly_event' );
}
//Create a custom hook and links a function to execute
add_action( 'wp_stocks_weekly_event', 'wp_stocks_update_all_stocks' );
Everything seems fine, right? The previous code is able to schedule the hook wp_stocks_weekly_event properly, but when it runs it does nothing (it is unable to call the function wp_stocks_update_all_stocks).
I have been using a cron jobs plugin to debug what’s happening and as I can see the previous piece of code schedules successfully the hook, but without calling any action (function):
An important note: When I move the previous code to theme’s functions.php everything works amazingly well and the code creates the hook with the functions attached to it, sending the e-mails every min as scheduled. So I guess the problem is that, for some reason, the function wp_stocks_update_all_stocks is not accessible outside the plugin environment.
Any idea of why this is happening and how can I solve that?
Cheers!
