I want to run a cron job that fetches data from an api and stores it in a database table.
I don't use "wp-cron". I load a script in a folder inside the theme. But I need to load the wordpress system somehow, so I have this code inside the script:
require_once( '../../../../../wp-load.php' );
I was thinking that I could require the wordpress system by requiring "wp-load.php
", (which is also done in "wp-cron.php") and after that I could import my custom classes which make use of $wpdb
and other wordpress stuff.
If I only run a simple database insert and nothing else (with normal mysql syntax), it works. But when adding the require statement , it fails. Do you know why? And do you know how I could make this work, so I can make use of the wordpress system?
EDIT The problem is with "require_once", so it's not wordpress related... If I try to require another script, and put my test database insert code there, that script never runs.
So in the main script I do:
require_once( 'hello.php' );
In "hello.php" I have the database insert (which works if I put it in the original file, without no require statements.
I use windows task scheduler to run the script, so maybe it's more related to that.
EDIT2:
I tried placing the script in the root folder, so the require became:
require_once( 'wp-load.php' );
and this time it worked! So the problem seems to be related to using paths going back to the parents ( ../../../etc
) . I also tried absolute paths but wasn't lucky.
../../../../../wp-load.php
is actually pointing to wp-load.php? If the script is in a directory directly under your theme root, you have one too many../
. You should also activate WP_DEBUG to give you some info on why it is crashing.