I have very simple file with one line:

echo file_get_contents( get_template_directory_uri().'/scheduler.php' );

This file is called by cpanel cron using php -q. When I put full link without get_template_directory_uri function everything works fine, with this function cron returns Call to undefined function get_template_directory_uri()

  • Are you usig a child theme? If yes try using get_stylesheet_directory_uri() instead of template directory – 4ndro1d Mar 15 '14 at 12:56
  • Nope there is no child theme – raaaa Mar 15 '14 at 12:57
  • 1
    what is the cron file you're using? It sounds like you're trying to use WordPress APIs without including WordPress ( there's no magic autoloader here ) – Tom J Nowell Mar 15 '14 at 13:26
  • 1
    Also, what is your cron script trying to do? – Tom J Nowell Mar 15 '14 at 14:06

When you run things on the CLI (which is pretty much where Cronjob and runwhen jobs are running), then you don't have access to some things like some server or request variable contents, etc. What you need to do is fire up WP Core. Else you won't have access to the full WP API.

One method would be to use the API from core, but disable WP internal (virtual/fake) Cron system by setting

define( 'DISABLE_WP_CRON', TRUE );

in your wp-config.php file. Then target [~/wp-cron.php] directly - where ~ would be your domain. Keep in mind that you might need to set the proper subdomain or subdirectory when you are running things in a Multisite/Network setup.

WordPress loads the whole core from within this file and then runs every defined (virtual) cron job during the real cron job.

Where it won't work (opening line inside /wp-cron.php):

// No connection to the browser/client needed
ignore_user_abort( TRUE );

if (
    ! empty( $_POST )
    OR defined( 'DOING_AJAX' )
    OR defined( 'DOING_CRON' )

Because you need to load WordPress first!

require 'path/to/wordpress/wp-load.php';
  • You are right it works if I include this. Is there any way to get dynamic path? – raaaa Mar 15 '14 at 13:31
  • 4
    from where? short of searching the entire file system that isnt answerable without more information – Tom J Nowell Mar 15 '14 at 14:04
  • I want to install theme on different servers without manually changing path to wp-load.php file. – raaaa Mar 15 '14 at 14:10
  • @user1756097 Please be more specific - provide additional info with an edit to your question. – kaiser Mar 15 '14 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.