I'm wondering whether if I can check a specific cron job is working or not. For example, I would like to send myself an email if the wpse_twicedaily_cron and wpse_oncedaily_cron is not active/ running in the WP.
I can setup the email, but I just need to figure out how to detect if certain cron job is active. I will need to check if the cron jobs are working every 5 minutes.
Thank you!
To view all scheduled tasks, you can use the code below that utilizes _get_cron_array(). Sample code is taken from here
function bl_print_tasks() {
echo '<pre>'; print_r( _get_cron_array() ); echo '</pre>';
}
bl_print_tasks();
You can then create your hook and function and add it as a cron to wordpress.
add_action( 'wpb_custom_cron', 'wpb_custom_cron_func' );
function wpb_custom_cron_func() {
// Check whatever here and then email
wp_mail( 'you#yourdomain.com', 'Cron Alert Email', 'Automatic scheduled email from WordPress to test certain cronjobs');
}
To add your own cron or simply see / trigger other crons, you can use WP Control plugin. Here is how to add your cron from Tools » Cron Events
Screenshot and sample mail code taken from here
Related
I know that to reduce the days and eliminate the records, this is done
function wca_reduce_action_scheduler_retention() {
return WEEK_IN_SECONDS;
}
add_filter( 'action_scheduler_retention_period', 'wca_reduce_action_scheduler_retention' );
However what I need is to discard the logs of the group rocket-preload
To be more precise it skips saving logs for that group. Since I don't need them.
This is possible?
This on the Wordpress Scheduled Actions page (/wp-admin/tools.php?page=action-scheduler)
function cronstarter_activation() {
if( !wp_next_scheduled( 'mycronjob' ) ) {
wp_schedule_event( strtotime( '00:02:00' ), 'daily', 'mycronjob' );
}
}
add_action('wp', 'cronstarter_activation');
function my_repeat_function() {
//My code the same information to database
}
add_action ('mycronjob', 'my_repeat_function');
Everything works great.
My problem is that I have php code in sidebar to recieve the storaged information from the database that saved from cron_job,
and the first time that a visitor visits the site in the new day after the 00:02:00 the recieved information is the previous day information.
I think that the first visitor of day, first see the sidebar's result, which is the previous day information, and after activate the cronjob.
Is that true? How can I run the cronjob without needed the first visitor?
Yes WP Cron have big caveat is that it requires someone to visit the site in order to run. For example, if you schedule your job to run at 2AM but no one visits at 2AM, it won’t run at 2AM. It will run when someone visits the site after 2AM.
More info Official : https://codex.wordpress.org/Function_Reference/wp_schedule_event
More info public blog:
http://brianshim.com/webtricks/cron-job-wordpress/
For more control on cron job use schedule at system level:
Use PHP to create, edit and delete crontab jobs?
Setup/configuration will differ based on your server & hosting type.
I have written an action that attaches to woocommerce_order_status_completed, and it works fine, adding a bit of meta data to the order. But the email that goes out after order completed seems to go BEFORE this runs, and therefore does not send the meta data in question (it will send it if I rerun the completed order again, but that is because this data is now already in the DB). So what I am looking for is either:
a hook that runs JUST before the completed email sends, OR
a way to have the completed email send AFTER woocommerce_order_status_completed hook
Any ideas or pointers? I looked through the Woocommerce API reference but can't find anything that seems to suit.
UPDATE: found an earlier hook and tried hooking it into
add_action( 'woocommerce_order_status_completed_notification','mysite_woocommerce_order_status_completed',5,1 );
which should run sooner, but STILL the email goes out first (before the meta data is in the DB and can be read. If I "recomplete" the order (putting it back into processing status and then completed again), it will send the meta data (again, this is because it is now in the db)
After much hair pulling, I have come up with a workaround which seems kind of ugly, but hopefully it will help someone else out.
I verified that my hook WAS correctly running before the main email one. (using add_action( 'woocommerce_order_status_completed_notification','mysite_woocommerce_order_status_completed',5,1 );
)
I verified that my meta data WAS correctly inserted into the db BEFORE the email went out
Unfortunately, it still refused to grab my meta data on first send. So I did the following:
I copied the woocommerce/templates/emails/email-order-items.php template into my theme and made the following change:
// Variation
if ( ! empty( $item_meta->meta ) ) {
echo '<br/><small>' . nl2br( $item_meta->display( true, true, '_', "\n" ) ) . '</small>';
// following 5 lines are MY extra code (checking for my meta field 'signup_code')
if (!array_key_exists('signup_code',$item_meta->meta)) {
$suc = wc_get_order_item_meta( $item_id, 'signup_code' );
if ($suc) {
echo '<br/><small>signup_code: ' . $suc . '</small>';
}}
}
It will check for a dupe in the meta array and not output if it already exists. It needs to do this to prevent it showing twice (which it would otherwise do on second send). I can't believe this is all necessary, but I can't find any other pointers anywhere that can address this.
UPDATE:
This was apparently caused by a woo internal caching problem. I had a lengthy discussion with one of the woo devs here:
https://wordpress.org/support/topic/hook-an-action-before-transactional-woocommerce-emails-are-triggeredsent-out/page/2?replies=40#post-8379725
And the upshot is, it will be fixed in a future version, but you can see the changes here:
https://github.com/woothemes/woocommerce/commit/3db3f3796eb28fb79983f605a5a70d61ca114c6d
I have this code:
// set up cron job
wp_schedule_event(time(), 'hourly', 'test_event');
add_action('test_event', 'testFunction');
function testFunction()
{
$query = new WP_Query(array(
'post_type' => 'tour',
'post_status' => 'publish'
));
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
echo $post_id . "a<br>";
}
}
And of course, I disabled the Wp cronjob define('DISABLE_WP_CRON', true);.
When I go to http://my-domain/wp/wp-cron.php?doing_wp_cron in the browser (the domain is local, so can't set the real cronjob in the crontab yet with wget), it outputs post IDs multiple times, like this:
1
2
3
1
2
3
Sometimes even more, and sometimes it causes memory error. It seems to me like the cronjob is running infinitely. I can't figure out why.
Not a WP pro, so any suggestion is appreciated!
Thanks.
wp_schedule_event registers a cron task with your WordPress installation.
So, what you're doing here, is every time you're running your cron you are telling WordPress to run your function an additional time to the last time.
If you've visited the cron page 10 times then your cron will be running 10 times per visit.
You can unschedule the event using wp_unschedule_event if you need to.
When you call wp_schedule_event, a new event is registered. You should only call that function once (events persist through multiple visits, globally), the documentation recommends scheduling after plugin activation.
"To schedule an hourly event in a plugin, call wp_schedule_event on activation (otherwise you will end up with a lot of scheduled events!)"
You'll want to use wp_unschedule_event to clear your current bindings, then call the function once. If it is part of your theme, use them activation and toggle your theme to something else and back.
i was working in a wordpress registration plugin. i stucked in expiry of the user. actually i want to expire the member after one year of his/her registration. and i want to notify them via email before 1 month of their expiry. i am using add_action('init','my function name') to check how many of the user is going to expire after a month and also to send the mail. bt this action hook will run every time a user visits the site which will make my site too slow to load everytime a user will visit. so i want something dat will make this code run once in a day. e.g. when the first user visit the site this code will run and for the whole remaining day this code will not be invoke no matter how many user will visit the website.
Wordpress has a built-in function/API that just do exactly what you want - doing something every day/hour/any interval you specify.
http://codex.wordpress.org/Function_Reference/wp_schedule_event
Taken shamelessly from the above page
add_action( 'wp', 'prefix_setup_schedule' );
/**
* On an early action hook, check if the hook is scheduled - if not, schedule it.
*/
function prefix_setup_schedule() {
if ( ! wp_next_scheduled( 'prefix_daily_event' ) ) {
wp_schedule_event( time(), 'daily', 'prefix_daily_event');
}
}
add_action( 'prefix_daily_event', 'prefix_do_this_daily' );
/**
* On the scheduled action hook, run a function.
*/
function prefix_do_this_daily() {
// check every user and see if their account is expiring, if yes, send your email.
}
prefix_ is presumably to ensure there will be no collision with other plugins, so I suggest you to change this to something unique.
See http://wp.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress/ if you want to know more.