2014-10-11 56 views
2

我試圖在wordpress以外的腳本上使用wordpress函數和$wbdb,但我無法弄清楚如何去做。通過擴展類來在其外部使用wordpress 4.0?

我想:

require_once('./wp-load.php'); // this is the correct path is tested. 
class cron extends wpdb {  
    public function results(){ 
     $sql = 'SELECT sub_id,email,cate_id FROM co_subsriber WHERE status = 0 ORDER BY sub_id ASC LIMIT '.$start.',750'; // $start =0 
     $records = $wpdb->get_results($sql); 
    } 
} 

我得到錯誤

Warning: Missing argument 1 for wpdb::__construct(), called in wp-db.php on line 578 
Warning: Missing argument 2 for wpdb::__construct() called in wp-db.php on line 578 
Warning: Missing argument 3 for wpdb::__construct() called in wp-db.php on line 578 
Warning: Missing argument 4 for wpdb::__construct() called in wp-db.php on line 578 
Notice: Undefined variable: dbuser wp-db.php on line 602 and all other pass, hostname... 

無法選擇數據庫....

我需要一提的是與

require_once('./wp-load.php'); 

並使用簡單的PHP,沒有面向對象它的工作正常。

那麼我應該擴展哪個類?

+0

在這裏閱讀http://codex.wordpress.org/Integrating_WordPress_with_Your_Website – Napolux 2014-10-11 08:35:21

+2

爲什麼你想擴展類呢?只需將它存儲在構造函數的類中並使用它。 – 2014-10-11 08:36:38

+0

@Napolux添加了'require('./ wp-blog-header.php');'同樣的錯誤。此外,它並沒有說明如何在你創建的課程中做到這一點。 – user3467855 2014-10-11 08:37:09

回答

5

問題是您不要使用正確的參數調用wpdb類的構造函數。

你需要做這樣的事情:

class cron extends wpdb { 

    function __construct() { 
    parent::__construct(/* params here */) 
    } 

} 

但是,這完全是因爲unnessecary是$wpdb在WP-load.php已經實例化

只是這樣做:

require_once('./wp-load.php'); 

class Cron { 

    private $wpdb; 

    function __construct($wpdb) { 
    $this->wpdb = $wpdb; 
    } 

    public function results() { 
    $sql = 'SELECT sub_id,email,cate_id FROM co_subsriber WHERE status = 0 ORDER BY sub_id ASC LIMIT '.$start.',750'; // $start =0 
    $records = $this->wpdb->get_results($sql); 
    } 
} 

現在你實現你的班級:

$cron = new Cron($wpdb);