1

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.

3
  • Have you checked that ../../../../../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.
    – Vlad Olaru
    Jan 28, 2018 at 18:34
  • Yes if I try to require a different file in the main directory and just echo something, and load the script in the browser, it is required correctly, so that path is working. I also tried to require wp-load , which worked when loading it in the browser. But I'm using windows task scheduler for the cron, and I can't see any errors.
    – Galivan
    Jan 28, 2018 at 19:10
  • related questions and possible answers here and here
    – Milo
    Jan 28, 2018 at 20:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.