I created a Linux Cron Job like:
0 0 * * * home/www/wp-content/themes/my_theme/functions.php
The file wp-content/themes/my_theme/functions.php has the function I need to call from the cron job: do_something().
As you can see the command is: home/www/wp-content/themes/my_theme/functions.php
is wrong, since I need to specify my function: do_something().
My question is: how do chage the job command to execute the function do_something().
what you're missing is the PHP
path, so this should work like this:
- Find out your PHP binary by running this command:
whereis php
- Add your function on an empty .php file e.g.
cron-functions.php
- Edit your cron so it looks like this:
# Replace "/path/to/php" bellow with your PHP binary location
0 0 * * * /path/to/php home/www/wp-content/themes/my_theme/cron-functions.php
In case you are wondering you can also pass in arguments or function calls via cron like:
# Pass function name as an argument, in this case our sample do_something() function.
0 0 * * * /path/to/php home/www/wp-content/themes/my_theme/cron-functions.php do_something
And that should be pretty much it, some times you may have issues with user permissions to run cron or PHP CLI processes if that is the case and this does not work just let us know so we can help you.
Good luck.
(1) Do not call functions.php file directly for cron job if you have wordpress specific function to use but setup wordpress environment first with two commands I mentioned in the previous question you had posted.
<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
//ensure correct *path* for your wp-blog-header.php file
//now you can use function you want from functions.php file directly
do_something();
?>
(2) If your do_something() function is not for any wordpress related task, just copy that function and use it in your cron file, say mycron.php
(3) You had asked where to put this suggested code. Put suggested code at the top of your mycron.php file.