Hej, I wrote a php script which was meant to be a WP-Cron-cronjob and which uses wordpress specific functions. Due to some restrictions in its runtime enviroment, I'm in need to start this script from cli with /usr/bin/php -q longThing.php instead of as a WP-Cron event. How can I ensure that all the wordpress core functions are callable in my script?

1 Answer 1


Xaedes solution in https://wordpress.stackexchange.com/a/76466/107596 works quite well:

    if( php_sapi_name() !== 'cli' ) {
        die("Meant to be run from command line");

    function find_wordpress_base_path() {
        $dir = dirname(__FILE__);
        do {
            //it is possible to check for other files here
            if( file_exists($dir."/wp-config.php") ) {
                return $dir;
        } while( $dir = realpath("$dir/..") );
        return null;

    define( 'BASE_PATH', find_wordpress_base_path()."/" );
    define('WP_USE_THEMES', false);
    global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
    require(BASE_PATH . 'wp-load.php');

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.

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