wordpress live site : include them class in a cron

2019-08-30 02:15发布

问题:

import a class from theme directory outside of wordpress.

The CRON I am cronning a daily job via Cpanel for my site that will feed a stats table. I created a file in the root Home/myDirectory (same level as public_html ) beloo is the code

<?php

include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
include dirname(__FILE__) . '/public_html/wp-content/themes/cooking/config/Data/ViewsData.php'; // require a class

function test(){

    if(class_exists('ViewsData')){
        $viewsData = new ViewsData;

        $views= $viewsData::getViews(394);

        update_post_meta(394 , 'views_test', $views);
    }

    die();
}

test();

THE CLASS

as shown in the code I am trying to include a class from the theme folder. this class include different functionaries to set , get and update the views data> bellow is an idea of how the class is structured ;

namespace GS\Data;
 use GS\DisplayFunc;

class ViewsData {

static function getViews(){}
}

however class_exists('ViewsData') always return false.

any suggestions on what is wrong. or even on how to reorganize the whole cron solution. the most important is that I need to use many classes from my theme folder.

回答1:

I was able to find the problem. it has to do with namespaces the code that works bellow :

<?php

include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
include dirname(__FILE__) . '/public_html/wp-content/themes/cooking/config/Data/ViewsData.php'; // require a class

function test(){

    if(class_exists('GS\Data\ViewsData')){
        $viewsData = new ViewsData;

        $views= $viewsData::getViews(394);

        update_post_meta(394 , 'views_test', $views);
    }

    die();
}

test();