Set Up a Background Task (“Cron Job”) in WordPress

Let’s say you have a page or piece of code that needs to be run regularly, say once a day, or once an hour.  It might update a database, import something into WordPress, clear out log files, make a backup, or whatever.

If you have access to the server, you can set up what Linux folks call a “cron job”, which runs any code you specify at any intervals that you specify based on the system clock.  You can set up a script to run every night at 2AM for example.

Now, many WordPress users do not have access to their servers because they use shared hosting or just don’t know how to access it.  (If you have server access and know how to create a real cron job, no need to read this article!)

So, I’ll explain how to do it without access to your server’s cron files.  You will need to do a bit of coding though!

One Big Caveat

Before the Linux folks start screaming, let me say that this “cron job” that we are setting up in WordPress is not a true cron job.  The 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.  If your site has tons of traffic, and you’re not too picky about exactly when the script runs, then this might be no problem.  Just be aware.

Now that we have that out of the way, let’s start creating it.

Creating the Job

First, install the WP Crontrol plugin. This will run a WordPress action hook of your choice at the specified intervals.

The next thing you’ll have to do is to create the hook.  You can do this in a plugin or your theme’s functions.php file.  If you’re using a theme that could be auto-updated, you should create a child theme first, and add the hook in your child theme’s functions.php file.

If the script you want to execute is in a WordPress page on your site (called “test-page” in this case), this is the code you would add to your theme’s (or child theme’s) functions.php:

function run_cron_script() {
    $dump = file_get_contents(get_home_url()."/test-page/");
    //wp_mail( "youremail@test.com", "Your cron job has run", $dump );  // FOR TESTING ONLY
}
add_action("my-cron", "run_cron_script");

Alternatively, you could add this to a plugin.  The commented-out email is for testing purposes, just to make sure it’s working.  I like to uncomment that the first time I run it.

Note that you could use an absolute URL if your script is not on the same domain.

Here is an example of the code if you want to run a particular file in a plugin directory:

function run_cron_script() {
    $dump = file_get_contents(plugin_dir_url(__FILE__)."cron_script.php");
    //wp_mail( "youremail@test.com", "Your cron job has run", $dump);   // FOR TESTING ONLY
}
add_action("my-cron", "run_cron_script");

This code should go in a file within the same plugin directory that the script that you want to execute is in.

Next, go to your site’s Admin to Tools -> Cron Events.  Scroll to the bottom under the “Add Cron Event” tab and enter the name of the WordPress action hook you created.  In the examples above, that would be “my-cron”.

Next to “Event schedule”, select the interval that you’d like the code to run at.

Next click “Add Cron Event”.  You should see your hook name in the list above.  Find it and click “Run Now”.

Your code should execute now.  If you wish, uncomment the email line in the code to receive an email when it runs to make sure it is working.  You can comment it out again later.  If your code takes a while to run, you’ll have to wait for it to complete before you get your email.

That’s it!  That’s how you set up a “background task” to run at regular intervals in WordPress!  How did it work for you? Please leave a comment below! – Brian

Shares

Please Leave a Question or Comment

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Inline Feedbacks
View all comments
Angelina Garbarino
Angelina Garbarino
7 years ago

Should we keep define( ‘DISABLE_WP_CRON’, false ); as false or do we need to change it to true?

Suzana
Suzana
7 years ago

Hy,

it works, but my cron file is coded to send e-mail and when i run cron, i get a lot of e-mails (it should be only one). When i run script without cron, then is ok. What should i do?