2

I am encountering a strange problem which I couldn't solve so far.

I'm calling a .php script via cronjob (Debian/GNU Linux). To use wordpress functionailty I added:

define('WP_USE_THEMES', false);
require( '/full/path/to/wp-blog-header.php' );

Now database queries and default wordpress functionality all works fine, but I also call some functions provided by a plugin (namely advanced custom fields, the problem however affects other plugins as well as I tested) the following php error is returned:

PHP Fatal error:  Call to undefined function update_field() in executed.php on line 24

Which tells me that the function of that plugin has not been included. So it seems to me that require wp-blog-header.php dies not include plugin functionality (which it did when I started to develop the mentioned script, but now when I tested it, it produced an php error that).

Could it be, that the 3.5 upgrade of wordpress changed something here? Or does anyone of you has some piece of advice for me as to why this problem occures? Thank you!

edit

I made further tests. The problem seems to be, that I am executing the php in the shell using following command:

/usr/bin/php5 -q -d memory_limit=256M /path/to/executed.php

Thus certain variables are not set like $_SERVER['REQUEST_METHOD'] Wordpress would therefore give me warnings in debug-mode. So what I did is, I definied the DOCUMENT_ROOT variable, and included my function.php (which was alos not loaded) manualy:

$_SERVER['DOCUMENT_ROOT'] = '/full/path/to/my/document/root/';
define('WP_USE_THEMES', false);
require( '/full/path/to/wp-blog-header.php' );
require_once( '/full/path/to/themes/mytheme/functions.php' );

Now it is working. Yet it seems to me as a mere unhealthy walkaround ...

  • 2
    Have you considered using a custom WP CLI command instead of manually boot strapping WordPress? – Tom J Nowell Feb 16 '15 at 11:59
0

You're probably calling the function before it's defined - why not hook into it?

Something like:

function someFunction() {
  require( '/full/path/to/wp-blog-header.php' );
} 
add_action('some_hook', 'someFunction');

Note: You might try the plugins_loaded hook.

  • Thank's for your suggestion. Since requiring the wp-blog-hreader.php sets up wordpress functions this solution doesn't work. The "add_actio" function has not been initialized. I tried to wrap my problematic function in your suggested one which resulted in this function not being executed at all. – lhermann Jan 5 '13 at 20:31
0

I'm not sure if this solution will work, but try require_once the Plugin's main PHP file into your cron-job PHP file or you may simply recreate the functionality of that plugin in your own PHP to get the tasks of that Plugin done.

0

Wordpress is meant to run from a webserver and not from a command line. While it is not obvious what kind of dependency might have caused this behaviours, those dependencies do exists and if you want to "run" wordpress from the command line you need to simulate the web server enviroment which is something that wp-cli probably dose and integration with it might have served you better than trying to invoke wordpress by yourself.

0

How about using your cronjob to call the Wordpress wp-cron.php?

Wordpress uses a quasi cron system that lets you schedule certain functions that are run when the website is accessed after the scheduled time. You can disable this "Do Cronjobs when Website is accessed" functionality and instead run a real cronjob to call the wp-cron.php like every 15 Minutes.

You then can hook the function that needs to run into the Wordpress cron scheduling, which means you should be able to use all the Wordpress Features including Plugin functions within this action.

Step 1: disable WP-Cron. Open your wp-config.php file. Go to the bottom of the database settings in wp-config.php (around line 37). Add the code: define('DISABLE_WP_CRON', 'true');

Step 2: Add a "real" cron job to call the wp-cron.php file as often as you need.

Step 3: Write yourself a plugin. In this plugin, code the functionality you need to run by the cron into a function.

function do_awesome_cron_stuff(){
   //do all the awesome things
}

Step 4: Hook your function up to the Wordpress Cron First, you register the scheduling start to the activation of the plugin:

register_activation_hook(__FILE__, 'awesome_cron_scheduling_activation');

function awesome_cron_scheduling_activation() {
    if (! wp_next_scheduled ( 'my_awesome_cron' )) {
          wp_schedule_event(time(), 'hourly', 'my_awesome_cron');
         //hourly means every hour (duh), there are more keywords to use for the time differences, you can find them here:
         //https://codex.wordpress.org/Function_Reference/wp_schedule_event
    }
}

For info, you don't directly schedule a function, but an action (here: my_awesome_cron). So now, we have to hook our function to the action

add_action('my_awesome_cron', 'do_awesome_cron_stuff');

For the last part: Deactivate the Cron on Plugin deactivation

register_deactivation_hook(__FILE__, 'awesome_cron_scheduling_deactivation');

function awesome_cron_scheduling_deactivation() {
    wp_clear_scheduled_hook('my_awesome_cron');
}

Thats it. Your cron will run and run and run and run until you disable the plugin ;) .

Happy Coding!

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.