Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Cron Job in Word Press

Is it possible to set up a Cron job to run on a WordPress site server so that I can update a custom database table at a specified time each day?

The site is hosted at WPEngine; seems that is VERY restrictive on what you can do.

Thanks.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

It depends on the hosting.  If it is available, it will usually show on whatever hosting control panel is available.  Or you could set it up if you have shell access but that seems unlikely at WPEngine.
You can use wp_cron() to get around hosting restrictions. There is also a plugin.
Avatar of Richard Korts

ASKER

In the example, it appears that my_task_function can be anything, is that correct?

So I could run a program located at the root of my site with like header("Location: http://mysite.com/run_this.php");

Am I interpreting that correctly?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of William Nettmann
William Nettmann
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Specifically what I want to do is this.

Every day the customer will upload a "flat" file obtained from some other source into a folder in the root directory of the site.

At 2:00 AM (for example, the exact time not yet defined), I want a cron job to execute a custom php script (this is OUTSIDE of WordPress) to read that flat file & update a custom table in the database.

I have already created the php script & run it manually, I just want to automate it.

It amazes me the obstacles WordPress puts in the way of trying to do something normal that you can do trivially in a normal LAMP environment.

Is "actual/path/to" the cwd value (relative path from the root?).

Thanks
Yes, the actual path of the file you want to run.

WordPress is actually providing you with more functionality than an empty LAMP environment. You are butting heads with the difference between running something on your own PC, where you have full control, and a shared web hosting server, where you have virtually no control.

If you had full control of the server, or even just a little more control, you could set up a cron job and that would do it for you.

Another possibility: how does the user upload the file?

If it is a form on a WordPress page, you could simply process the files and write it to the database immediately.

This would be my first choice.
William Nettmann

Thanks for all the info. I am NOT talking about running something on my own PC. I am talking about a shared web server. I have been building custom php / MySQL apps since BEFORE WordPress existed. I know how to do a cron setup on a linux / unit server. Most of the time the host "cpanel" gives you an app to do that.

I will try the way you are suggesting. Hope it works.

Thanks
Doesn't WPEngine provide a cpanel of some sort?

I only  wanted to point out it's not WordPress that's blocking you, you seem to be thinking that from your comments.
William Nettmann

William Nettmann,

I cannot find a control panel per se on WPEngine.

I chatted yesterday BEFORE I did this post with someone there who indicated it could not be done.

So maybe they are wrong, but putting all these layers in between an experienced developer & the "real code" is NOT an advantage.

You seem very Word Press knowledgeable. Can you look at my other current open (unanswered) WP question on EE right now?

This is reflective of everything I have discovered about Word Press. If what you need to do doesn't fit the mold of Word Press & the theme, good luck, it's a tough road.
If what you need to do doesn't fit the mold of Word Press & the theme, good luck, it's a tough road.
That's the consequences of using something that was made to be 'easy'.  Because to make it 'easy' and still work, you have to limit the options that are available.
Dave Baldwin,

Thanks Dave,

I'm kind of aware of that.

I have found WordPress both a boon (some themes) and a curse.

But I understand it's here to stay, I have to live with it & work with it if I want to stay in business.

As I understand it, roughly 20% of ALL websites WORLD WIDE are in Word Press
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jason,

I'd love to move it, I have a host that is fantastic, they will not restrict & do GREAT tech support. I host other customized WP sites there.

But the customer chose WPEngine; I have to feel out their sensitivities about that. Also, there may be monetary considerations.
To all,

I tried the Nettman way. Maybe I did it wrong. Here is what I did.

(1) I found out the path to the root of the site using getdwd().

(2) I created a php program called write_file.php. See attached.

(3) I added this to functions.php (in the child theme):

if ( ! wp_next_scheduled( 'my_task_hook' ) ) {
  $time = strtotime('17:45'); // Time to run each day;
  wp_schedule_event( $time, 'daily', 'my_task_hook' );
}

add_action( 'my_task_hook', 'my_task_function' );

function my_task_function() {
  exec('php /nas/content/staging/watermanusa/write_file.php>/dev/null 2>&1');
}

I picked 17:35 as it was about 17:25 central time; I assume the host is in Austin Texas.

(4) I visited the site.

It does nothing (the file testfile.txt does not exist).

What's wrong?
write_file.php
To all,

I communicated with WPEngine. They said to use plugin wp-crontrol.

Haha funny name.

I installed the plugin. It could run from a "hook" or from php code. I chose the hook, didn't do anything (using the "hook" from functions.php as discussed earlier). So then I said from php code, copied the code, ran it & IT WORKED!!

Thanks to both of you for your help!!
It does nothing (the file testfile.txt does not exist).

What's wrong?

WPEngine blocks exec() calls.  It's a security issue by their standards.

Glad to hear a plugin worked.