0

I am a bit new to wp-cron job.

I think i understand the concept but lack the implementation. i read a lot of tutorials on how to implement including this post: Run function at specific time but still cant figure where (in which files) is best practice.

The above link describes kind of well my issue. i want each day on a specific hour to run test.php file which is in my root folder of wordpress(where the wp-content and include and etc).

i am well aware for the disadvantages of wp-cron (no traffic no cron) and i am fine with it. i dont want to workaround with unix cron.

I want the test.php file to be called not earlier then a specific time each day. please help and specify the file in which the implementation should be made.

thanks

1

It depends what you mean by "I want the test.php file to be called not earlier then a specific time each day".

Setting up a cron job is very straightforward, you just need to simply do:

<?php
register_activation_hook( __FILE__, 'prefix_activation' );
/**
 * On activation, set a time, frequency and name of an action hook to be scheduled.
 */
function prefix_activation() {
    // Schedule job for 3pm every day
    wp_schedule_event( mktime(15, 0, 0, date("n"), date("j"), date("Y")), 'daily', 'prefix_daily_event_hook' );
}

add_action( 'prefix_daily_event_hook', 'prefix_do_this_daily' );
/**
 * On the scheduled action hook, run the function.
 */
function prefix_do_this_daily() {
    // do something every day
}

What it is you decide to do inside prefix_do_this_daily is up to you.

  • thank you, on which file this should be? should it be in function.php? or independent file which will be included in function? i want to http get the test.php file – Meschiany Jul 22 '14 at 12:47
  • Yes, inside your functions.php file – mikemike Jul 22 '14 at 12:48
  • thank you, i dont manage to find a way to fire a php file from root, i tried:wp_remote_get( "/test.php"); – Meschiany Jul 23 '14 at 10:47
0

You can run WP cron manually by calling: http://example.com/wp-cron.php?doing_cron

If you don't want the automatic cron to run while you're debugging, then add this to your /wp-config.php file:

define('DISABLE_WP_CRON', true); If you're on a development environment and want to output debug information, calling it manually like that will show you your debug output.

Alternatively you can use PHP's built-in error_log function to log message strings to the error log for debugging. You'd need to use this in conjunction with WP_DEBUG settings.

Also Here are some links which may help you for setting up a cron

Cronjob

Working Example

  • thank you, i dont mind it to be fire on the next user page view after my specific time, my issue is more on where should i implement @mikemike 's code – Meschiany Jul 22 '14 at 12:49
-1

I am using crony now and it works very well.

https://wordpress.org/plugins/crony/

With this cron one can easily set php file to fire or custom code on your own intervals. this instead of other plugins which helps you with setting the custom intervals rather the code and schedule itself. (plug and play - no code changes is required)

  • 1
    Sorry, but this is not an answer. Please add an explanation. – fuxia Sep 14 '14 at 9:53
  • You are right, sorry for that. hope that is better – Meschiany Sep 14 '14 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.