2014-10-11 55 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);