wordpress live site : include them class in a cron job

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 14:26:35

问题


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();


来源:https://stackoverflow.com/questions/56019183/wordpress-live-site-include-them-class-in-a-cron-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!