Using plugin functionality in external php script not working

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 …

4 Answers
4

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.

Leave a Comment