通过扩展一个类,在它之外使用wordpress 4.0?

时间:2014-10-11 08:34:14

标签: php wordpress

我正在尝试在WordPress之外的脚本上使用wordpress函数和$wbdb,但我无法弄清楚如何去做。

我试过了:

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,没有OOP与类,它工作正常。

那么我应该扩展哪个班级?

1 个答案:

答案 0 :(得分:5)

问题是你没有用正确的参数调用wpdb类的构造函数。

你需要做这样的事情:

class cron extends wpdb {

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

}

但这完全是不可靠的,因为$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 );