微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Wordpress中从functions.php的外部文件运行类?

如何解决如何在Wordpress中从functions.php的外部文件运行类?

如何从functions.PHP的外部文件中运行类?

我正在使用wordpress和Beaver Builder插件我有一个分页列表,其中包含来自WP db的信息。这是使用搜索模板(search.PHP)的页面。它包含一个文件(get-jobs.PHP)。为了查看结果,我必须使用PHP Snippet插件页面上设置一个全局变量,以识别由get-jobs.PHP提供的$ result变量,因为我使用内置模块来布局页面

到目前为止,该方法有效。我可以看到格式化的结果。

单击分页按钮时,将从我的js文件(custom-script.js)中调用一个函数以更新结果。

function selectJobs() {
    $.ajax({
        url: site_url + '/get-jobs.PHP',type: "GET",data: {
          ajax': '1','state': GetURLParameter('country'),'limit': GetURLParameter('limit')
        },success:function(data) {
          console.log(data);
        },error: function(errorThrown){
          console.log(errorThrown);
        }
    }); 
}

这不起作用,我收到403(禁止错误。我将其更改为:

function selectJobs(country) {
    var country = country;
    $.ajax({
        url: ajaxurl,type: "POST",data: {
          'action':'country_ajax_request','country' : country
        },error: function(errorThrown){
          console.log(errorThrown);
        }
    }); 
}

...摆脱了错误,并提交了请求。

在我的functions.PHP中,我有这个:

function country_ajax_request() {
   //include_once( get_stylesheet_directory() .'/get-jobs.PHP');

   MY FUNCTIONS GO HERE
}
add_action( 'wp_ajax_country_ajax_request','country_ajax_request' );
add_action( 'wp_ajax_nopriv_country_ajax_request','country_ajax_request' );

这是get-jobs.PHP中的一些内容

<?PHP

$get = (!empty($get) ? $get : $_REQUEST);

if(isset($get['ajax']) && $get['ajax'] == 1) {
  require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.PHP');
}

class GetJobs {

    private $wpdb;

    public function __construct($input) {
        global $wpdb;
        $this->wpdb = $wpdb;
        $this->tableName = $this->wpdb->prefix . 'job_postings';
        $this->country = (!empty($input['country']) ? $input['country'] : '');
        $this->page = (!empty($input['page']) ? $input['page'] : '');
        $this->limit = (!empty($input['limit']) ? $input['limit'] : 10);
    }

    public function getLocations() {
        $result = [];
        $states = $this->wpdb->get_results("SELECT country FROM " . $this->tableName . " WHERE deleted_at = '0000-00-00 00:00:00'");

        foreach ($country as $key=>$row) {
            $result[$row->country] = $row->country;
        }

        return $result;
    }
    
    // almost 200 more lines of this
}

$get['page'] = (!empty($get['page']) ? $get['page'] : 1);

$jobs = new GetJobs($get);

$query = $jobs->getQuery();

$jobsQuery = $jobs->getJobsQuery($query,(!empty($get['limit']) ? $get['limit'] : $jobs->limit));

$result['postings'] = $jobs->formatPostings($jobsQuery);

if(isset($get['ajax']) && $get['ajax'] == 1){
    echo json_encode($result);
}

我试图包含该文件,但这没有用。它提取了数据,但似乎没有遍历所有功能

我想重用现有文件来执行更新列表任务。如果可能的话,我不想重写get-jobs.PHP。我在其他项目上也使用过相同的代码。只是没有这个插件

感谢任何可以解决该问题的想法或帮助。

谢谢。

更新

我再次查看了该包含。它正在提取数据。但是,它会生成一个json文件,随后是所有html。 HTML格式不正确。这是开始的样子:

<div id="postings" style="display: block;">{"states":{"AB":"AB","AZ":"AZ","BC":"BC","CA":"CA","FL":"FL","GA":"GA","IL":"IL","KS":"KS","NC":"NC","NS":"NS","ON":"ON","PA":"PA","QC":"QC","WA":"WA"},"category":{"":"","Accounting":"Accounting","Customs":"Customs","Finance":"Finance","Human Resources":"Human Resources","IT":"IT","information Technology":"information Technology","International":"International","Marketing":"Marketing","Marketing and Communications":"Marketing and Communications","Operations":"Operations","Project Management":"Project Management","Reception":"Reception","Sales":"Sales","Warehouse":"Warehouse"},"postings":"<a href="\&quot;\/career?jid=job_20200827220600_9WZICSJNQ2XVDB1N\&quot;">\n\t\t\t\t\t</a>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。