1

I did created a cron job for a script in my wordpress site which is using a theme. When the script is called with cron it is not working fine. Some function which are in that script are not recognized, because script not see them and report them as undefined function.

Fatal error: Call to undefined function get_option()

Do i need to include something more, or how to make that script to run normally with cron job ?

Jigberto
  • 1,513
  • 7
  • 27
  • 50
  • did you include the wp stuff? did you include them with an absolute path? remember that cron jobs have a different working directory than scripts running under a webserver. – Marc B May 09 '13 at 20:11
  • 1
    You should probably consider using [**wp_cron**](http://codex.wordpress.org/Function_Reference/wp_cron), the psuedo cron function that works with Wordpress. – adeneo May 09 '13 at 20:11
  • Is `get_option()` defined in the same file that the Cron Job is running? If not, then you are probably referring to the file that contains `get_option()` via the `$_SERVER` variable (e.g.: `require_once($_SERVER['DOCUMENT_ROOT'] . 'path/to/file')`. However, [the `$_SERVER` variable is empty](http://stackoverflow.com/questions/2100545/serverdocument-root-does-not-work-in-the-php-script-running-through-cron) whenever a page is loaded via a Cron Job. You might want to try using `__file__` instead. – Chris May 09 '13 at 20:14
  • 1
    @Chris - get_option() is defined in functions.php if I remember correctly. It's a built in WP function. – adeneo May 09 '13 at 20:16

1 Answers1

1

You can use wp_cron(), Wordpress's built in cron function: http://codex.wordpress.org/Function_Reference/wp_cron

Otherwise, the issue with get_option not being recognized is probably because Wordpress core is not loaded. You need to require wp-load.php in your external php script with the absolute path to that file:

require_once("/path/to/wordpress/wp-load.php");

Ennui
  • 10,102
  • 3
  • 35
  • 42